logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matt Emory <repairman2...@gmail.com>
Subject Re: Set log name in appender of log4net
Date Fri, 20 Aug 2010 14:55:06 GMT
You can set the filename in the appender to take a property

<appender>
  ...
  <file type="log4net.Util.PatternString value="property{FileName}" />
</appender>

and set it in code via:
log4net.GlobalContext.Properties["FileName"] = logName;

or you can set it in code with something like this if you're dynamically
creating appenders:

 log4net.Appender.RollingFileAppender appender=
LogManager.GetRepository().GetAppenders()
                    .Where(appender=>appender.Name ==
source).FirstOrDefault() as log4net.Appender.RollingFileAppender;

if(appender != null)
{
   appender.File = logName;
   appender.ActivateOptions();
}


On 8/20/10, Damian Zembalski <damianzet1@wp.pl> wrote:
>
> I have class MyLogger, where I use log4net. How can I modify my appender
> to save log in specific logname (I want to set it by parameter logName).
>
>
>
>
>    public void AddEntry(string source, string logName, string contextInfo,
> string message, EventLogEntryType eventType)
>            {
>
>                 log4net.ILog Log = log4net.LogManager.GetLogger(source);
>
>                 Log.Error(String.Format("Context Info: {0}{1}{2}{3}",
> contextInfo,
> Environment.NewLine, Environment.NewLine, message));
>
>            }
>
>    <log4net>
>      <root>
>        <priority value="ALL" />
>         <appender-ref ref="EventLogAppender" />
>      </root>
>
>
> This is myAppender. Now it writes in common logtype Application.
>
>      <appender name="EventLogAppender"
> type="log4net.Appender.EventLogAppender" >
>
>        <layout type="log4net.Layout.PatternLayout">
>          <conversionPattern value="%logger (%property{myContext}) [%level]-
> %message%newline" />
>        </layout>
>      </appender>
>    </log4net>
>
>
>

Mime
View raw message