Yes, there are already a few posts out there, and yet I think there is value in providing just a recipe to make it work in your ASP.NET project without too many further details. So here you go (in C# where code is used):
Step 1: Download log4net, version 1.2.10 or later, and unzip the archive
Step 2: In your project add a reference to the assembly log4net.dll.
Step 3: Create a file log4net.config at the root of your project (same folder as the root web.config). The following content will log everything to the trace window, e.g. “Output” in Visual Studio:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="TraceAppender" type="log4net.Appender.TraceAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d %-5p- %m%n" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="TraceAppender" />
</root>
</log4net>
</configuration>
Step 4: In AssemblyInfo.cs add the following to make the resulting assembly aware of the configuration file:
// Tell log4net to watch the following file for modifications: [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
Step 5: In all files in which you want to log add the following as a private member variable:
private static readonly log4net.ILog Log =
log4net.LogManager.GetLogger(
MethodBase.GetCurrentMethod().DeclaringType);
Step 6: Log as needed. For example for testing that steps 1 to 5 were successful, add the following in file Global.asax.cs:
public class Global : HttpApplication {
protected void Application_Start(object sender, EventArgs e) {
Log.Info("Application Server starting...");
}
}
For more information about configuring log4net, e.g. logging to files, see log4net’s web site.
As always, if you find a problem with this recipe, please let me know. Happy coding!
1 comments:
Thanks for simple straight article. I saved my time.
Post a Comment
All comments, questions and other feedback is much appreciated. Thank you!