Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use log4net in .net

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use log4net in .net, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

Step 1 of creating logging

Add a reference to log4net.dll, of course.

The second step

Add the following sentence to the AssemblyInfo.cs file:

[assembly: log4net.Config.XmlConfigurator (ConfigFile = "Web.config", Watch = true)]

This means that log4net automatically looks for the configuration file App.config or Web.config to get and load the configuration information in it. If you want log4net to monitor the configuration file for reloading at any time, write as above (the winform program ConfigFile is App.config).

The third step

Configure Web.config.

The above configuration is taken from the Duke of Zhou, and the description of the configuration has been annotated in more detail.

The fourth step

Record information in the program. We add the following code to the Page_Load method of Default.aspx under the project:

/ / get log4net instance ILog log = log4net.LogManager.GetLogger ("LogFileAppender"); log.Info ("PageLoad method under Default.aspx is executed")

Then run the project and you can find that a log message has been recorded in c:\ log4netfile.txt:

Recording time: 2011-04-21 21 21 LogFileAppender property 26 null 17729 Thread ID: [4] Log level: INFO error Class: LogFileAppender property: [(null)]-error description: the PageLoad method under Default.aspx is executed

With the above steps, I think you have been able to create some simple logging capabilities for the program. Let's talk about some other usage and features of log4net in detail.

Log4net details

Here I will explain some of the applications of log4net in the form of questions and answers.

There are several ways to record 1.log4net. How to configure and use each method?

The output methods supported by Log4net currently include:

1 AdoNetAppender

Log to the database. You can use both SQL and stored procedures.

2 AnsiColorTerminalAppender

Write a high-brightness log event at the ANSI window terminal.

3 AspNetTraceAppender

You can view recorded logs as Trace in asp.net.

4 BufferingForwardingAppender

Cache log events before outputting to a child Appenders.

5 ConsoleAppender

Output the log to the console.

6 EventLogAppender

Write the log to Windows Event Log.

7 FileAppender

Write the log to a file.

8 LocalSyslogAppender

Write the log to local syslog service (for UNIX environments only).

9 MemoryAppender

Save the log to the memory buffer.

10 NetSendAppender

Output the log to Windows Messenger service. This log information will be displayed in the dialog box of the user terminal.

11 RemoteSyslogAppender

Logs are written to Remote syslog service through the UDP network protocol.

12 RemotingAppender

Write the log to the remote receiver through the .NET Remoting.

13 RollingFileAppender

Write the log to the file as a rollback file.

14 SmtpAppender

Write the log to the message.

15 TraceAppender

Write the log to the .NET trace system.

16 UdpAppender

Send the log connectionless UDP datagrams to the remote host or broadcast it as UdpClient.

You can see that there are still many ways to support it at present. Here I will tune a few that I think are commonly used as an example.

1. We have already talked about the method of the document above, and we are not going to give an example here.

two。 Database mode:

First, add the database record appender

Then set up the c:log4net.mdb database and enable the database recording mode in the code:

/ / get log4net instance ILog log = log4net.LogManager.GetLogger ("AdoNetAppender_Access"); log.Debug ("PageLoad method under Default.aspx is executed")

In this way the log is recorded in the database.

How does 2.log4net filter the log information I want, such as only program error logs in a log?

This requirement can be achieved by configuring filter. The specific operations are as follows:

Global approach:

This approach applies the level to all log entry methods. To do this, add: under the root node:

In this way, for all logging methods, all regions at the ERROR level will not be recorded.

Configure the level separately:

This method will not affect other log input methods. The method is as follows: add filter under the specific appender:

In this way, you can configure the orientation of the record level, and if you only need to record one, the maximum and minimum settings are the same.

3. I don't like the log output format above. How can I configure the log output format I want in log4net?

The input format for log4net is defined in the layout of each appender. Specifically, there are the following built-in options:

% m (message): output log messages, such as ILog.Debug (…) An output message

% n (new line): line feeds

% d (datetime): outputs the time the current statement is running

% r (run time): output the number of milliseconds consumed by the program from run to execution to the current statement

% t (thread id): thread ID on which the current statement resides

% p (priority): the current priority of the log, namely DEBUG, INFO, WARN … Etc.

% c (class): the name of the current log object, for example:

% f (file): the file name where the output statement is located.

% l (line): the line number where the output statement is located.

% number: represents the minimum length of the item. If it is not enough, fill it with spaces. For example, "%-5level" means that the minimum width of level is 5 characters, and if the actual length is less than 5 characters, fill with spaces.

With these things, you can combine whatever output format you like.

4. Is there a way to control the program to automatically record log information by date, that is, every day's log is in a different log file?

In fact, this method corresponds to a specific recording method: RollingFileAppender. This approach is also based on file recording, but it provides a more flexible logging method. Specifically, he can automatically change the log file according to the size of the log file or the time of the log record.

Classify records according to different dates of each day:

Transform according to the size of the log file, which can effectively reduce the expansion of the log file:

The above is all the content of the article "how to use log4net in. Net". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report