Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to make some adjustments to the the name of the file and the header/footer sections of the log file in my FileAppender logger.

Given the following config section:
XML
<appender name="FileAppender" type="log4net.Appender.FileAppender">
  <file value=".\ImportLog.log"/>
  <appendToFile value="true"/>
  <layout type="log4net.Layout.PatternLayout">
    <param name="Header" value="== Begin Processing ===================="/>
    <param name="Footer" value="== Completed Processing ================"/>
    <conversionPattern value="%date{HH:mm:ss},%message,%-5level,%logger%newline"/>
  </layout>
</appender>


I tried to do this:
<file value=".\%date{HHmmss}ImportLog.log"/>

...but my filename was literally "%date{HHmmss}ImportLog.log".

I want my header and footer to have a line space above and below them, so I tried this:
XML
<param name="Header" value="== Begin Processing ====================%newline"/>
<param name="Footer" value="== Completed Processing ================%newline"/>

...but I just get the text "%newline" after those lines. In this instance, \r\n also appear as literals.

Is there a way to template or (modify at runtime) these non-log lines?
Posted
Comments
TheyCallMeMrJames 22-Sep-10 14:48pm    
btw...I tried to use the tags 'logging' or 'log4net', which don't exist, so if someone has the power, might those be good tags?

1 solution

Found a solution[^]: derive from PatternLayout.

I ended up doing this:
C#
public class TimestampedPatternLayout : PatternLayout
{
    public override string Header
    {
        get
        {
            string result = "== Processing Started ({0}) ==============\r\n";
            result = string.Format(result, DateTime.Now.ToString("HH:mm:ss"));
            return result;
        }
        set { }
    }
    public override string Footer
    {
        get
        {
            string result = "== Processing Ended ({0}) ================\r\n";
            result = string.Format(result, DateTime.Now.ToString("HH:mm:ss"));
            return result;
        }
        set { }
    }
}


Which also simplified my config:
XML
<appender name="FileAppender" type="log4net.Appender.FileAppender">
  <file value=".\ImportLog.log"/>
  <appendToFile value="true"/>
  <layout type="Engage.ImportLegacy.TimestampedPatternLayout">
    <conversionPattern value="%date{HH:mm:ss},%message,%-5level,%logger%newline"/>
  </layout>
</appender>


Cheers.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900