English 中文(简体)
How do I use a date pattern in a header/footer?
原标题:
  • 时间:2009-11-10 22:45:51
  •  标签:
  • log4net

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: %date{MM/dd/yy HH:mm} ]").

<appender name="RollingLogFileAppender"
          type="log4net.Appender.RollingFileAppender">
  <file value="C:somelog" />
  <appendToFile value="true" />
  <rollingStyle value="Date" />
  <datePattern value="-yyyy-MM-dd .txt " />
  <layout type="log4net.Layout.PatternLayout">
    <header value="[START:  %date{MM/dd/yy HH:mm} ]&#13;&#10;" />
    <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} - %message" />
    <footer value="[END]&#13;&#10;&#13;&#10;" />
  </layout>
</appender>

How can I get this to print the date/time in the header?

最佳回答

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} ]
", 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.

问题回答

Answer from here.

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

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.

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>

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} ] ", dateString))

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

E.g

===Start===
[START:  2012-02-23 12:12:12 ]

logs...




相关问题
Log4Net and extra fields

Is it possible to insert extra fields into the database and use them in log4net? I have a UserId I would like to have in an extra field in the log-table. I have added the field in the log4net.config:...

log4net with .NET 4.0

I ve thrown together some code to tinker with the new .Net 4.0/VS 2010 pieces, but I can t seem to find a build of my logging framework of choice (log4net) for 4.0, and I m getting reference errors ...

Best logging approach for composite app?

I am creating a Composite WPF (Prism) app with several different projects (Shell, modules, and so on). I am getting ready to implement logging, using Log4Net. It seems there are two ways to set up the ...

log4net log files disappear when service restarted

We are using log4net to create our logfiles from Windows services, and we are using the RollingFileAppender rolling based on date. The version of log4net we are using is 1.2.9. Now for the issue. ...

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

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: %date{MM/dd/yy HH:mm} ]"). <...

热门标签