logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Pareto, Charles" <Charles.Par...@va.gov>
Subject RE: how do I define log file at run time?
Date Tue, 13 Jul 2010 18:23:11 GMT
I figured out how to change the name of the log file using a pattern to
set the value of the string property, but how do I change the filename
in my source code.

Here is all my code:

private readonly ILog logger = LogManager.GetLogger("DeIdentifyDicom");
//defined at top of class

log4net.Config.XmlConfigurator.Configure(); // defined in constructor

logger.Fatal("Failed to Open File in Time Limit. Filename - " +
filePath); //using it to log in methods

 

 

Here is the .config file:

 

<log4net>

    <appender name="ConsoleAppender"
type="log4net.Appender.ConsoleAppender">

      <layout type="log4net.Layout.PatternLayout">

        <conversionPattern value="%date [%thread] %-5level %logger
[%property{NDC}] - %message%newline" />

      </layout>

    </appender>

    <root>

      <level value="INFO" />

      <appender-ref ref="ConsoleAppender" />

    </root>

 

    <logger name="DeIdentifyDicom">

      <level value="INFO" />

      <appender-ref ref="LogFileAppender" />

    </logger>

    

    <appender name="LogFileAppender"
type="log4net.Appender.FileAppender">

      <file type="log4net.Util.PatternString"
value="c:\log-file-[%processid].txt" />

      <param name="AppendToFile" value="true"/>

      <layout type="log4net.Layout.PatternLayout">

        <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd
hh:mm:ss} - %m%n" />

      </layout>

    </appender>

</log4net>

 

 

From: Pareto, Charles [mailto:Charles.Pareto@va.gov] 
Sent: Tuesday, July 13, 2010 10:47 AM
To: log4net-user@logging.apache.org
Subject: how do I define log file at run time?

 

I have just started using log4net and I can write to my c# application.
I'm currently writing to the log file "c:\DeIdentifyDicom.log"

Defined in my .config file, but I would like to set the log file name at
runtime. Can anyone help me with this?

Here would be an example and the name of the log I would need to create
at runtime.

 

Void SomeMethod()

{

String scanCode = ReadScanCode();

//create log called scancode.log

//Log to scancode.log

}

 

 

 

Here is the .config file info I have so far, but this only creates a
staticly named log file

 

<configSections>

    <section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>

  </configSections>

 

  <log4net>

    <appender name="ConsoleAppender"
type="log4net.Appender.ConsoleAppender">

      <layout type="log4net.Layout.PatternLayout">

        <conversionPattern value="%date [%thread] %-5level %logger
[%property{NDC}] - %message%newline" />

      </layout>

    </appender>

    <root>

      <level value="INFO" />

      <appender-ref ref="ConsoleAppender" />

    </root>

 

    <logger name="DeIdentifyDicom">

      <level value="INFO" />

      <appender-ref ref="LogFileAppender" />

      <!--<appender-ref ref="SmtpAppender" />-->

    </logger>

    <appender name="LogFileAppender"
type="log4net.Appender.RollingFileAppender" >

      <param name="File" value="c:\DeIdentifyDicom.log" />

      <param name="AppendToFile" value="true" />

      <rollingStyle value="Size" />

      <maxSizeRollBackups value="10" />

      <maximumFileSize value="10MB" />

      <staticLogFileName value="true" />

      <layout type="log4net.Layout.PatternLayout">

        <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd
hh:mm:ss} - %m%n" />

      </layout>

    </appender>

  </log4net>

 


Mime
View raw message