Reputation: 549
I've been shown how to use log4net and got a config file to use. I lost this config file and now I cant get it to work. For some reason the guy who configured it could not get it to work while having the configuration info in app.config so he placed it in log4net.config in the output folder.
He then called this when the program started up:
var logConfig = Path.Combine(Path.GetDirectoryName(typeof(App).Assembly.Location), "log4net.config");
var configStream = new FileInfo(logConfig);
log4net.Config.XmlConfigurator.Configure(configStream);
And it worked flawlessly.
Now I got it to work perfectly in a new console project. However allmost all of my projects are addons to another application so my project types are class libraries loaded by this other application (called Revit).
Anyway, so I know that I can log, but just cant get it to work... This is log4net.config that I use:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<file value="logs\" />
<datePattern value="dd.MM.yyyy'.log'" />
<staticLogFileName value="false" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="5MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender"/>
</root>
</log4net>
Each class that I want to log stuff from declares the an ILog instance on the class level. Like this:
static private ILog _logger = LogManager.GetLogger(typeof(Program));
The ILog instance reports that all logging is enabled (debug and so on), but there are no files created.
Do I need to do anything else?
Im going nuts here! Thanks for any help!
Upvotes: 0
Views: 1045
Reputation: 27944
It seems you tried a few different ways to configure log4net. If one fails, the best option is to enable log4net debugging to see if your logger is working/crashed. In your app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
...
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\tmp\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
Upvotes: 1
Reputation: 250
In App.config file you should add this code in order to register the log4net section handler and the log will be generated
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
......
</configuration>
or Check if your AssemblyInfo.cs file has this code :
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
Upvotes: 0