C# log4net configuration

The log component of the project is a must-have and is required in any project. This is convenient for both the development and testing in the early stage and the maintenance of the project in the later stage of the project. A good log component of C# project is log4net . Below I will briefly introduce how to configure log4net in website projects and desktop application projects .

Configuration in the website project

First, you have to make sure your computer is connected to the Internet. After ensuring this, we first install log4net in the project . There are many ways to install, I will briefly introduce the method I often use: Open the project with Visual Studio 2013 (I am using 2013 here) , find the "Reference" right mouse button in the project directory of "Solution Explorer" Click "Manage NuGet Packages (N)..." in the pop-up menu, and a management window will pop up as shown below:

figure 1

If this does not work, you can try the "Package Manager Console" to install with the command: first open the console, in "Tools""NuGet Package Manager""Package Manager Console" , you can open it Package Manager Console.

So what if the install command won't? This is easy to do or do the following operations like the window in Figure 1 :

figure 2

image 3

Figure 4

In this way , the log4net package is installed into the project, and the next step is how to configure it in the project. First, explain how the website project is configured.

Step 1: Do the following configuration in the Web.config file, first take a look at the picture above:

Figure 5

Image 6

The following is the code in the configuration file in Figure 5 and Figure 6:

copy code

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

  <log4net>
    <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
    <!-- Set root logger level to ERROR and its appenders -->
    <root>
      <level value="ALL" />
      <appender-ref ref="SysAppender" />
    </root>
    <!-- Print only messages of level DEBUG or above in the packages -->
    <logger name="WebLogger">
      <level value="DEBUG" />
    </logger>
    <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net">
      <param name="File" value="App_Data/" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <param name="DatePattern" value=""Logs_"yyyyMMdd".txt"" />
      <param name="StaticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      </layout>
    </appender>
    <appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net">
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      </layout>
    </appender>
  </log4net>

copy code

View Code

The second step, first paste the code below:

Figure 7

The code in Figure 7:

//Configure log4
            log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~/Web.config")));

View Code

In this way, the configuration of log4net in the website project is completed.

The last step is how to apply it in the project code. I will also briefly talk about it, that is, it is simple to use, and it is not complicated. Or speak the same as above:

Figure 8

Figure 9 below is the log generated in the project:

Figure 9

The configuration of log4net in the website project has been completed. There are many other configuration methods on the Internet. Here I will only explain the simple configuration methods that I use more often.

Configuration of log4net in desktop application, console application, windows service project

Step 1: Install the log4net package into the project in the same way as the above website project configuration, in two ways. I won't repeat the explanation here, and if you don't understand anything, just read the above.

Step 2: The same is true, but the code is still the same from Web.config to App.config file, as shown in Figure 10:

Figure 10

Step 3: It's a little different. We need to add a line of code to AssemblyInfo.cs in the project:

//log4net reads the configuration from the configuration file
[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension = "config", Watch = true)]

Figure 11:

Figure 11

The last step is to use it in the project, which is the same as the use in the website project.

The configuration work of log4net is all explained. I hope it will be helpful to everyone. If there are deficiencies and mistakes, I hope to correct and understand! ! !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324772740&siteId=291194637