Alex Rivera | Logout

use log4net without web.config

Asked 2011-01-14T22:59:23.193
9

I am using log4net to log the errors.

Currently I downloaded the file log4net.dll, then made the following change in global.asax file:

private static ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

protected void Application_Start(object sender, EventArgs e)
{
    log4net.Config.XmlConfigurator.Configure();
}
protected void Application_Error(object sender, EventArgs e)
{
    Log.Fatal("An uncaught exception occurred", this.Server.GetLastError());
}

also these changes were made in the web.config file

<log4net>

<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
  <param name="File" value="c:\Log4NetExample.log"/>
  <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
  </layout>
</appender>

<root>

  <level value="All"/>

  <appender-ref ref="LogFileAppender"/>
</root>

everything works.. but what I need is to move the code from the web.config and read it from a text file instead.

Also I need 2 levels of debug and error..

Can anyone help me with this?

Thank you

Edit
Report

1 Answer

1

I don't know if this is what you want, but you can put the same log4net xml configuration information into a txt file, read it yourself, and then initialize log4net with exactly that xml.

See this question here on SO for how to configure log4net with xml programmatically. In the example, I am showing to send xml directly to log4net via the log4net api. All that is missing for your case is reading it from a text file. I think that if you copied your log4net xml configuration information from the web.config and pasted it into a text file, you should be able to read it from the file and send it to log4net as shown in the example.

Note that if you just want your log4net logging configuration in a separate file from the web.config, log4net does allow you to specify a separate file containing only the log4net configuration information (and this would be a more standard thing to do than my example above).

See this question for an example of how to do that for an ASP.NET application.

answered 2011-01-14T23:08:28.650

Your Answer