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 net Log Library in C #

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to use the net log library in C#". In the daily operation, I believe many people have doubts about how to use the net log library in C#. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the net log library in C#"! Next, please follow the editor to study!

Catalogue

I. brief introduction

Download log4net

3. Add log4net.dll reference

4. Add logs and configure log.config

Add log4net.dll-related parameters to the AssemblyInfo.cs file

VI. Test examples

6.1 Test code

6.2 effect

VII. Attached

7.1 more log configuration

7.2 Log format

7.3 Log configuration

I. brief introduction

Note-simple use of log4net log open source library: control log file size, number of log files, scroll overwrite, free control log printing level

Download log4net

Directly use the log4net source code or compile the corresponding dll with the log4net source code or use log4net.dll directly

Download address: http://logging.apache.org/log4net/download_log4net.html

The example uses dll of 4. 5 directly. (please select according to the. Net framework framework version of the current environment, see the right-click item-- "Properties--" Application-- "Target Framework)."

3. Add log4net.dll reference

4. Add logs and configure log.config

Edit the log.config as follows:

Synchronously update the configuration to the exe output path

Add log4net.dll-related parameters to the AssemblyInfo.cs file

Add a line at the end of the AssemblyInfo.cs file (where log.config is the configuration file added in step 4)

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

Test example 6.1 Test code namespace Log4netTest {public partial class Form1: Form {/ / Mylog1 corresponds to a logger name of the log.config species public static readonly log4net.ILog mLogger = log4net.LogManager.GetLogger ("Mylog1"); public Form1 () {InitializeComponent () } private void Form1_Load (object sender, EventArgs e) {mLogger.Error ("APP Start"); mLogger.Debug ("logDebug"); mLogger.Info ("logInfo"); mLogger.Warn ("logWarn"); mLogger.Error ("logError"); mLogger.Error (this);}}

Note: the Mylog1 of the code species corresponds to a logger name in log.config

6.2 effect

Control log file size, number of log files, scroll overwrite, control log printing level to WARN.

7. 7.1 more log configuration

Http://logging.apache.org/log4net/release/manual/configuration.html

7.2 Log format

% 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: indicates the minimum length of the item. If it is not enough, fill it with spaces. For example, "%-5level" indicates that the minimum width of level is 5 characters. If the actual length is less than 5 characters, fill with spaces.

7.3 Log configuration

There can be multiple appender and logger tags, not one-to-one correspondence.

At this point, the study on "how to use the net log library in C#" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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