How do I use a date pattern in a header/footer?

后端 未结 4 671
醉梦人生
醉梦人生 2020-12-28 20:13

Here\'s my appender configuration from my app.config. This just prints out the literal string instead of translating it to the date (i.e., it literally prints \"[START: %d

相关标签:
4条回答
  • 2020-12-28 20:53

    Building on @Wizou's comment. See the DynamicPatternLayout Class documentation.

    I'm actually using this difference in behaviour to have a start and end time

    One important difference between PatternLayout and DynamicPatternLayout is the treatment of the Header and Footer parameters in the configuration. The Header and Footer parameters for DynamicPatternLayout must be syntactically in the form of a PatternString, but should not be marked as type log4net.Util.PatternString. Doing so causes the pattern to be statically converted at configuration time and causes DynamicPatternLayout to perform the same as PatternLayout.

    Setting the type on Header to PatternString but leaving Footer as dynamic

    <layout type="log4net.Layout.DynamicPatternLayout"> <param name="Header" value="%newline**** Trace Opened Local: %date{yyyy-MM-dd HH:mm:ss.fff} UTC: %utcdate{yyyy-MM-dd HH:mm:ss.fff} ****%newline" type="log4net.Util.PatternString" /> <param name="Footer" value="**** Trace Closed %date{yyyy-MM-dd HH:mm:ss.fff} ****%newline" /> </layout>

    0 讨论(0)
  • 2020-12-28 20:57

    I encountered this problem and a quick workaround I used is to just use a fixed header and footer. Then do this as soon as you have the ILog object:

    log.Info(string.Format("[START: {0} ]\r\n", dateString))

    So just under the header you will get the information you desire.

    E.g

    ===Start===
    [START:  2012-02-23 12:12:12 ]
    
    logs...
    
    0 讨论(0)
  • 2020-12-28 21:01

    An easy way to do this is to subclass log4net.Layout.PatternLayout and override Header and Footer. Then you can add whatever you want to your Header: date stamp, machine name, user name, assembly version, or whatever your heart desires:

    using System;
    using log4net.Layout;
    
    namespace MyAssembly
    {
        class MyPatternLayout : PatternLayout
        {
            public override string Header
            {
                get
                {
                    var dateString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    return string.Format("[START:  {0} ]\r\n", dateString);
                }
            }
        }
    }
    

    Include this new class in your assembly, and use the new type in your file, like this:

    <layout type="MyAssembly.MyPatternLayout">
        <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} - %message" />
    </layout>
    

    Since you overrode Header and Footer, you don't even need to add it here. The Header will be added every time your application starts, and every time the file rolls over.

    0 讨论(0)
  • 2020-12-28 21:06

    Answer from here.

    <header value="[BEGIN LOGGING AT %date]%newline" type="log4net.Util.PatternString" />
    <footer value="[END LOGGING AT %date]%newline" type="log4net.Util.PatternString" />
    

    Works for me like a charm. No need to write a piece of code.

    Also in projects we usually use:

    <header type="log4net.Util.PatternString" value="Our Application Name version %property{Assembly.Version}, .NET version %property{Runtime.Version}, %date ***%newline"/>
    

    Take a look at PatternString doc also.

    Also I've noticed that log file won't appear until you write first log statement.

    0 讨论(0)
提交回复
热议问题