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

偶尔善良 提交于 2019-11-30 04:06:16
pduncan

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.

Puterdo Borato

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.

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...

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>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!