在C#项目中配置log4net

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36150051/article/details/78893523
参考地址:https://www.codeproject.com/Tips/1107824/Perfect-Log-Net-with-Csharp


1 以consoleapplication为例


添加log4net的包
Add log4net refer to your project. We have 2 ways for adding that library. The first one is download the log4net from the internet to your local device, then add the reference to DLL file. The second one is download it from Nuget. I preferred this way by the following:


Right clicks on your project
Click Manage NuGet Packages…
Clicks on Browse on the left hand
Type log4net in search box under Browse option
Select log4net package and click Install


2
Open AssemblyInfo.cs file under Properties, then add the below line of code under the 
在Assembly配置文件中的这个
[assembly: AssemblyCulture("")] line.
一行下面加上,下面这一行。
Hide   Copy Code
// Let log4net know that it can look for configuration in the default application config file
[assembly: log4net.Config.XmlConfigurator(Watch = true)] 







在appconfig中加上下面的配置
Open the App.config file, by default after creating the console app, it will contain the following code:


Hide   Copy Code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" 


    sku=".NETFramework,Version=v4.5.2" />
  </startup>  
</configuration>
Next, add the configuration code for log4net into this file, but don't forget to add those configurations before <startup> </startup> tag to make sure the log4net runs well. After that, it should be:


Hide   Shrink    Copy Code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" 


    type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
  </configSections>


  <log4net>
    <appender name="TestAppender" 


    type="log4net.Appender.RollingFileAppender" >
      <file value="E:\log\MyTestAppender.log" />
      <encoding value="utf-8" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <!--<rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="5MB" />
      <staticLogFileName value="true" />-->
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %level [%thread] %type.%method - %message%n" />
      </layout>
    </appender>
    <root>
      <level value="All" />
      <!-- If the following line is not included the log file 
      will not be created even if log4net is configured with this file. -->
      <appender-ref ref="TestAppender" />
    </root>
  </log4net>


  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  
</configuration>
I'm going to explain a little for the above configured.




 4 编写调用日志的程序


using System;
using log4net;
using System.Threading;


namespace Log4Net.CSharp
{
    class Program
    {
        //Declare an instance for log4net
        private static readonly ILog Log = 
              LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        static void Main(string[] args)
        {
            ImplementLoggingFuntion();
        }


        private static void ImplementLoggingFuntion()
        {
            /* We have 5 levels of log message. Let's test all.
             *  FATAL
                ERROR
                WARN
                INFO
                DEBUG
             */
            var secs = 3;
            Log.Fatal("Start log FATAL...");
            Console.WriteLine("Start log FATAL...");
            Thread.Sleep(TimeSpan.FromSeconds(secs)); // Sleep some secs


            Log.Error("Start log ERROR...");
            Console.WriteLine("Start log ERROR...");
            Thread.Sleep(TimeSpan.FromSeconds(secs)); // Sleep some secs


            Log.Warn("Start log WARN...");
            Console.WriteLine("Start log WARN...");
            Thread.Sleep(TimeSpan.FromSeconds(secs)); // Sleep some secs


            Log.Info("Start log INFO...");
            Console.WriteLine("Start log INFO...");
            Thread.Sleep(TimeSpan.FromSeconds(secs)); // Sleep some secs


            Log.Debug("Start log DEBUG...");
            Console.WriteLine("Start log DEBUG...");
            Thread.Sleep(TimeSpan.FromSeconds(secs)); // Sleep some secs


            Console.WriteLine("Press any key to close the application");
            Console.ReadKey();
        }
    }
}


5 注意修改app.config中的
<file value="E:\log\MyTestAppender.log" /> 这个文件时对应日志的输出文件
阅读更多

没有更多推荐了,返回首页