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.开发者_StackOverflow中文版.
Can anyone help me with this?
Thank you
Put this attribute in your AssemblyInfo.cs file:
[assembly:log4net.Config.XmlConfigurator(ConfigFile="Log4Net.config",Watch=true)]
where 'Log4net.config' equates to the name of your config file.
There are of course, other ways to achieve the same effect. You can, for instance, leave the config section handler set up in your web.config. Move the element to its own xml file, and replace it in web.config with an empty element, thus:
<log4net configSource="c:\config\myLog4NetConfig.xml" />
The configSource attribute supplies the location of the external file (can't be a UNC path, though, it's got to be local to the machine.)
As far as configuring log4net...that can get a wee bit tricky. See these links for help and examples:
- http://logging.apache.org/log4net/release/manual/configuration.html
- http://weblogs.asp.net/tgraham/archive/2007/03/15/a-realistic-log4net-config.aspx
- http://haacked.com/archive/2005/03/07/ConfiguringLog4NetForWebApplications.aspx
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.
精彩评论