Log4net has lots of options and different ways it could be configured - these are notes written to myself so that I can get it working quickly.
1. Download log4net
Download from here.
2. Drop DLL in Bin folder
In the download package, there are various DLLs, usually you'll want the .net 2.0 release version at:
3. Add a Configurator line in the Global.asax
There are various ways of telling Log4net to pick up its config. We're going to use the Xml Configurator that pulls config from a separate config file. Add this line of code to Application_Start in the Global.asax:
The XmlConfigurator.Configure() call above, tells Log4net to look for a certain key in appSettings. The key gives the name of the config file. So we add the correct appSetting entry to web.config, like this:
In the root folder (same folder as web.config), add the following logging.config file:
6. Add logging statements
One good pattern for this is to have a static logger for each page:
So ... thats one way of doing it. There are, of course, millions of other ways.
1. Download log4net
Download from here.
2. Drop DLL in Bin folder
In the download package, there are various DLLs, usually you'll want the .net 2.0 release version at:
incubating-log4net-1.2.10\log4net-1.2.10\bin\net\2.0\releaseIf you have an ASP.NET Web Application Project (as opposed to an ASP.NET Web Site) then you'll also need to add a reference to the DLL.
3. Add a Configurator line in the Global.asax
There are various ways of telling Log4net to pick up its config. We're going to use the Xml Configurator that pulls config from a separate config file. Add this line of code to Application_Start in the Global.asax:
void Application_Start(object sender, EventArgs e) { log4net.Config.XmlConfigurator.Configure(); }4. Add an AppSetting to the web.config
The XmlConfigurator.Configure() call above, tells Log4net to look for a certain key in appSettings. The key gives the name of the config file. So we add the correct appSetting entry to web.config, like this:
<appSettings> ... <add key="log4net.Config" value="logging.config" /> ...5. Add a logging.config file
In the root folder (same folder as web.config), add the following logging.config file:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <log4net> <appender name="LogFileAppender" type="log4net.Appender.FileAppender"> <param name="File" value="App_Data/Log.txt" /> <param name="AppendToFile" value="true" /> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d thread%-2t %-5p src:%-25c - %m%n" /> </layout> </appender> <root> <level value="ALL" /> <appender-ref ref="LogFileAppender" /> </root> </log4net> </configuration>This has basic options for logging to a text file in the App_Data folder (make sure you have an App_Data folder). Its set to log everything, because during development thats what you'll want to do, right?
6. Add logging statements
One good pattern for this is to have a static logger for each page:
public partial class MyWebPage : System.Web.UI.Page { private static readonly ILog logger = LogManager.GetLogger(typeof(MyWebPage)); protected void Page_Load(object sender, EventArgs e) { logger.Debug("page load"); } ...
So ... thats one way of doing it. There are, of course, millions of other ways.
Comments