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

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

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

It is very convenient to use log4net to print logs. In the past, I used to write my own log function to write error information to the specified file. I omitted these tasks with log4net, which is very convenient.

Using log4net requires configuration in the .config file

The configuration file can use the default configuration file in the project, or you can create a new .config.

1. Use the default profile

Add [assembly: log4net.Config.XmlConfigurator ()] to AssemblyInfo.cs, and the system looks for the configuration node of log4net in the default configuration file at run time.

Write a help class and create a log object using singleton mode

/ / /

/ log4net help class

/ / /

Public class LogHelper

{

Private static ILog logInfo = null;// normal log

Private static ILog logDebug = null;// exception information

Private static ILog logError = null;// error log

Private static ILog logFatal = null;// fatal error

Private static ILog logWarn = null;// warning message

Private LogHelper () {}

/ / /

/ / General Log

/ / /

/ / /

Public static ILog GetLogInfo ()

{

If (logInfo = = null)

{

LogInfo = LogManager.GetLogger ("LogInfo")

}

Return logInfo

}

/ / /

/ / abnormal information

/ / /

/ / /

Public static ILog GetLogDebug ()

{

If (logDebug = = null)

{

LogDebug = LogManager.GetLogger ("LogDebug")

}

Return logDebug

}

/ / /

/ / error log

/ / /

/ / /

Public static ILog GetLogError ()

{

If (logError = = null)

{

LogError = LogManager.GetLogger ("LogError")

}

Return logError

}

/ / /

/ / fatal error

/ / /

/ / /

Public static ILog GetLogFatal ()

{

If (logFatal = = null)

{

LogFatal = LogManager.GetLogger ("LogFatal")

}

Return logFatal

}

/ / /

/ / warning message

/ / /

/ / /

Public static ILog GetLogWarn ()

{

If (logWarn = = null)

{

LogWarn = LogManager.GetLogger ("LogWarn")

}

Return logWarn

}

}

Call:

LogHelper.GetLogInfo () .Info ("output information")

LogHelper.GetLogError () .Error (", new Exception ())

two。 Use the newly created profile

Public static readonly ILog logInfo = LogManager.GetLogger ("LogInfo"); / / normal log

Public static readonly ILog logDebug = LogManager.GetLogger ("LogDebug"); / / exception information

Public static readonly ILog logError = LogManager.GetLogger ("LogError"); / / error log

Public static readonly ILog logFatal = LogManager.GetLogger ("LogFatal"); / / fatal error

Public static readonly ILog logWarn = LogManager.GetLogger ("LogWarn"); / / warning message

Manually find the configuration file under the path, read it, and add the following code

/ / /

/ / load the log configuration file for log4net

/ / /

/ / the full path to the log configuration file

Public LogHelper (string configFilePath)

{

If (string.IsNullOrWhiteSpace (configFilePath) | |! File.Exists (configFilePath))

{

Throw new ArgumentNullException (log profile does not exist)

}

XmlConfigurator.Configure (new FileInfo (configFilePath))

}

Call:

LogHelper.logInfo.Info ("output information")

LogHelper.logError.Error ("output information", new Exception ())

At this point, the study of "how to use log4net" 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

Internet Technology

Wechat

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

12
Report