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:54:25 GMT
I think I’m close but when I try the code below it writes a log file named ‘(null)’ with
a size of 0

Any thoughts?

 

Here is my code:

private readonly ILog logger = LogManager.GetLogger("DeIdentifyDicom");

log4net.Config.XmlConfigurator.Configure();

log4net.GlobalContext.Properties["LogName"] = "scancode.log";

logger.Fatal("Failed to Open File in Time Limit. Filename - " + filePath);

 

.config file

<?xml version="1.0"?>

<configuration>

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

      <file type="log4net.Util.PatternString" value="C:\%property{LogName}" />

      <appendToFile value="true" />

      <rollingStyle value="Size" />

      <maxSizeRollBackups value="-1" />

      <maximumFileSize value="5000KB" />

      <staticLogFileName value="true" />

      <countDirection value="1"/>

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

        <conversionPattern value="%m%n" />

      </layout>

      <filter type="log4net.Filter.PropertyFilter">

        <Key value="Version" />

        <StringToMatch value="1" />

      </filter>

      <filter type="log4net.Filter.DenyAllFilter" />

    </appender>

  </log4net>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

 

 

 

 

From: Yuriy Taraday [mailto:yorik.sar@gmail.com] 
Sent: Tuesday, July 13, 2010 11:29 AM
To: Log4NET User
Subject: Re: how do I define log file at run time?

 

Hello.

 

var appender = new log4net.Appender.FileAppender(log4net.Layout.PatternLayout.DetailConversionPattern,
scanCode+".log");

log4net.Config.BasicConfigurator.Configure(appender);

On Tue, Jul 13, 2010 at 10:23 PM, Pareto, Charles <Charles.Pareto@va.gov> wrote:

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