I have a console application with log4net, and I would like to add logfile name from code. (I use开发者_开发问答 threads later)
class Program
{
static void Main(string[] args)
{
{
log4net.GlobalContext.Properties["fname"] = "aaaa";
log4net.Config.XmlConfigurator.Configure();
}
}
}
<appender name="default" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="d:\\TEMP\\default_%property{fname}.log"/>
...
</appender>
And I get (null).
Thanks for your help.
It works for me - I used log4net 1.2.11 Following is the sample configuration I'm using (I used the same C# code as yours):
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="default_%property{fname}.log"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="100KB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
</layout>
</appender>
<root>
<priority value="ALL"/>
<appender-ref ref="RollingFileAppender"/>
</root>
</log4net>
</configuration>
This should work
I used log4net 1.2.13.0. One thing you should consider is adding a internal debug for log4net which will tell you the actual error.
class Program
{
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args)
{
log4net.GlobalContext.Properties["fname"] = "aaaa";
log4net.Config.XmlConfigurator.Configure();
log.Debug("Test");
}
}
Config section
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<log4net debug="true">
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="D:\default_%property{fname}.log"/>
<appendToFile value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
</layout>
</appender>
<root>
<priority value="ALL"/>
<appender-ref ref="RollingFileAppender"/>
</root>
</log4net>
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="E:\USERS\vivek.meka\Documents\Visual Studio 2015\Projects\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
精彩评论