. Net Core log4net usage

1, Background

Front row tips, friends who think ink can directly see the solution part!

Ah, I haven't spent so much time and effort for a long time... After a whole afternoon of drumming and consulting data, I finally succeeded in configuring log4net. However, the author doesn't know the bottom of log4net. Here is just a simple record of the learning process to provide some convenience for the same beginners.

2, Exploration process

In the process of consulting the data, I found some concepts, but they didn't work in the end. It may be the reason for the version change, or it may be simply useless.

1.log4net. The node configuration in the config configuration file is optional

<configsections>
  <section name="log4net" type="log4net.config.log4netconfigurationsectionhandler, log4net" />
</configsections>

2. It is also a configuration file, which contains the logger. If there are no special requirements, you don't need to set the logger tag separately. You can share the root tag configuration

3. The current version is net core 5.0 or 6.0 net framework is not very useful, and the directory structure of the two large versions of the framework is somewhat different. Some log4net strategies on the Internet are based on the Framework version. You can see such a sentence in it

"You can add the following statement in the assemblyinfo.cs file of the project"

[assembly:log4net.config.domconfigurator(configfile="filename",,watch=true/false)]

This file is not available in the core. Then the author tries every means to find out where this sentence should be placed in the core version

The final result is that in the core version, this sentence should be written in the form of "label" on the namespace, and it can be written globally once.

After the author's experiment, it is true. He was ecstatic after his success. But later, it was found that this sentence was useless, and there was no need to add.....

4. There is another pit. At that time, the author did the experiment on winform. In the configuration tag, use LogFileAppender to output to the local file. Then, the author found that using ConsoleAppender can output log to the command line, but the experiment was unsuccessful. Later, it was found that output to the command line refers to the command line project, not to the output of the project......

5.log database building, this is not used, of course, but it is not used by yourself

3, Solution

1. First download log4net package with nuget (nuget function has been integrated in VS), and the project will be successful if it can be referenced normally

2. Here, the author gives the configuration files extracted from the Internet, including some of the author's own modifications, but excluding the database configuration. There are many descriptions of configuration files on the Internet. You can find them by yourself. Just a little partner who wants to practice, just copy and use it directly. The generated log file is in the running directory

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<log4net >
		<appender  name ="LogFileAppender"  type ="log4net.Appender.FileAppender" >
			<file  value ="log4netfile.txt"   />
			<appendToFile  value ="true"   />
			<layout  type ="log4net.Layout.PatternLayout" >
				<conversionPattern  value ="%date %-5level %logger property:[%property{NDC}] - description : %message%newline"   />
			</layout >
		</appender >
		<appender  name ="ConsoleAppender"  type ="log4net.Appender.ConsoleAppender" >
			<layout  type ="log4net.Layout.PatternLayout" >
				<conversionPattern  value ="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"   />
			</layout >
		</appender >
		<appender  name ="EventLogAppender"  type ="log4net.Appender.EventLogAppender" >
			<layout  type ="log4net.Layout.PatternLayout" >
				<conversionPattern  value ="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"   />
			</layout >
		</appender >
		<appender  name ="AdoNetAppender_Access"  type ="log4net.Appender.AdoNetAppender" >
			<connectionString  value ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:log4net.mdb"   />
			<commandText  value ="INSERT INTO LogDetails ([LogDate],[Thread],[Level],[Logger],[Message]) VALUES (@logDate, @thread, @logLevel, @logger,@message)"   />
			<parameter >
				<parameterName  value ="@logDate"   />
				<dbType  value ="String"   />
				<size  value ="240"   />
				<layout  type ="log4net.Layout.PatternLayout" >
					<conversionPattern  value ="%date"   />
				</layout >
			</parameter >
			<parameter >
				<parameterName  value ="@thread"   />
				<dbType  value ="String"   />
				<size  value ="240"   />
				<layout  type ="log4net.Layout.PatternLayout" >
					<conversionPattern  value ="%thread"   />
				</layout>
			</parameter>
			<parameter>
				<parameterName  value ="@logLevel"   />
				<dbType  value ="String"   />
				<size  value ="240"   />
				<layout  type ="log4net.Layout.PatternLayout" >
					<conversionPattern  value ="%level"   />
				</layout>
			</parameter>
			<parameter>
				<parameterName  value ="@logger"   />
				<dbType  value ="String"   />
				<size  value ="240"   />
				<layout  type ="log4net.Layout.PatternLayout" >
					<conversionPattern  value ="%logger"   />
				</layout>
			</parameter>
			<parameter>
				<parameterName  value ="@message"   />
				<dbType  value ="String"   />
				<size  value ="240"   />
				<layout  type ="log4net.Layout.PatternLayout" >
					<conversionPattern  value ="%message"   />
				</layout>
			</parameter>
		</appender>
		<root >
			<level value="ALL"/>
			<appender-ref  ref ="LogFileAppender"   />
			<appender-ref  ref ="ConsoleAppender"   />
			<appender-ref  ref ="EventLogAppender"   />
			<appender-ref ref="AdoNetAppender_Access" />
		</root>
	</log4net>
</configuration>


3. In the main method, initialize the configuration

XmlConfigurator.Configure(new FileInfo("Yours log4net.config Profile location"));

4. in the class that wants to use log, first instantiate log, then call the corresponding method, input the information that you need.

//This is instantiation
private static readonly ILog log = LogManager.GetLogger(typeof(Put the current class name here));

//This is using
log.info("Information you need");

5. Log can be used in the above steps. The method officially provided by log4net is used here, which is the instantiation step. If you have more detailed requirements, you can write your own package and instantiate your own.

Keywords: C# .NET microsoft log4net

Added by casty on Tue, 01 Mar 2022 04:49:23 +0200