Graphic and text code sharing on how to use log4net under .NET

黄舟
Release: 2017-07-18 10:45:43
Original
1940 people have browsed it

This article mainly introduces the use of log4net under .net in detail. Taking the console application as an example, it has certain reference value. Interested friends can refer to the examples in this article

I have shared with you how to use log4net under .net for your reference. The specific content is as follows

Here is a console application as an example

The first thing to do is to add a reference:

After installation, you can see that there are more references to log4net in the project:

Add the application configuration file app.config and configure log4net


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <configSections>
 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
 </configSections>
 <log4net>
 <!-- Define some output appenders -->
 <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <!--日志路径-->
  <file value="test.txt"/>
  <!--是否向文件中追加日志-->
  <appendToFile value="true"/>
  <!--日志保留天数-->
  <maxSizeRollBackups value="10"/>
  <!--每个文件的大小。只在混合方式与文件大小方式下使用。超出大小后在所有文件名后自动增加正整数重新命名,数字最大的最早写入。可用的单位:KB|MB|GB。不要使用小数,否则会一直写入当前日志-->
  <maximumFileSize value="1024KB"/>
  <!--按照何种方式产生多个日志文件(日期[Date],文件大小[Size],混合[Composite])-->
  <rollingStyle value="Size"/>
  <!--否只写到一个文件中-->
  <staticLogFileName value="true"/>
  <layout type="log4net.Layout.PatternLayout">
  <!--记录时间:%date 线程ID:[%thread] 日志级别:%-5level 记录类:%logger  操作者ID:%property{Operator} 操作类型:%property{Action}%n    当前机器名:%property%n当前机器名及登录用户:%username %n    记录位置:%location%n 消息描述:%property{Message}%n     异常:%exception%n 消息:%message%newline%n%n-->
  <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
  </layout>
 </appender>
 <root>
  <level value="DEBUG"/>
  <appender-ref ref="RollingLogFileAppender"/>
 </root>
 </log4net>
</configuration>
Copy after login

Add code in Program.cs:


static void Main(string[] args)
{
 log4net.Config.XmlConfigurator.Configure();
 //创建日志记录组件实例
 ILog log = log4net.LogManager.GetLogger(typeof(Program));
 //记录错误日志
 log.Error("发生了错误:", new Exception("log4net的测试错误信息"));
 //记录致命的错误
 log.Fatal("发生了致命的错误:", new Exception("log4net测试致命信息"));
 //记录一般信息
 log.Info("log4net的一般信息");
 //记录调试信息
 log.Debug("log4net的调试信息");
 //记录警告信息
 log.Warn("log4net警告信息");
 Console.WriteLine("ok");
 Console.ReadKey();
}
Copy after login

Run the program

This is the console application. If it is a Web application, you can call log4net.Config.XmlConfigurator.Configure in the Application_Start method in Global.asax.cs (); Define a variable in Global.asax.cs, get the exception in Application_Error and log it:


public class Global : System.Web.HttpApplication
{
 private static ILog log = LogManager.GetLogger(typeof(Global));

 protected void Application_Start(object sender, EventArgs e)
 {
  log4net.Config.XmlConfigurator.Configure();
 }

 protected void Session_Start(object sender, EventArgs e)
 {

 }

 protected void Application_BeginRequest(object sender, EventArgs e)
 {

 }

 protected void Application_AuthenticateRequest(object sender, EventArgs e)
 {

 }

 protected void Application_Error(object sender, EventArgs e)
 {
  log.Error("发生了异常",Server.GetLastError());
 }

 protected void Session_End(object sender, EventArgs e)
 {

 }

 protected void Application_End(object sender, EventArgs e)
 {

 }
}
Copy after login

The above is the detailed content of Graphic and text code sharing on how to use log4net under .NET. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!