1

I am creating different reports and trying to pass the report name and append it to the name of the log4net report. The report name will be different each time the report is run so it will be dynamically assigned.
I've tried using the LOG4NET.GLOBALCONTEXT.PROPERTIES("RptName") = sReportName", but all I get is a 'null' value in the report title.

class Program
{
    static void Main(string[] args)
    {
        log4net.Config.XmlConfigurator.Configure(); 
        log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));

        log4net.GlobalContext.**Properties("RptName")** = sReportName
        log.Info("this is an info message"); 
        Console.ReadLine(); 
    }
}
    <!--RollingFileAppender:  Provides output of all log entries to a file in the \logs directory
          Filename:  RSGetReport_yyyyMMdd_hh:mm:ss_processID.txt-->  
  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString"  value="logs\RSGetReport_%date{yyyyMMdd}_%date{hhmmss}_%processid**_%property{myContext}.**txt" />
      <appendToFile value="true" />
      <rollingStyle value="Once" />
      <maxSizeRollBackups value="10" />
    <filter type="log4net.Filter.LevelRangeFilter">
      <levelMin value="ALL" />
    </filter>
      <maximumFileSize value="1MB" />
      <countDirection value="1" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
      </layout>
    </appender>strong text
Philipp M
  • 1,877
  • 7
  • 27
  • 38
Susan
  • 1,822
  • 8
  • 47
  • 69

1 Answers1

0

You need to set the global property before configuring log4net. See also this question.

Community
  • 1
  • 1
Stefan Egli
  • 17,398
  • 3
  • 54
  • 75