In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use the .NET Log Framework Nlog". In the daily operation, I believe many people have doubts about how to use the .NET Log Framework Nlog. 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 Framework Nlog". Next, please follow the editor to study!
NLog is a simple and flexible .NET logging class library. By using NLog, we can output contextual (contextual information) debug diagnostics in any .NET language, configure its presentation style according to preference and send it to one or more output targets (target).
Quick installation
Use GUI or the following command in the package Manager console:
1. Install Nlog
Install-Package Nlog
two。 Install Nlog.Config
Install-Package Nlog.Config Quick configuration
Open the Nlog.Config file in the directory and notice that there are detailed instructions in the XML file. The rules area allows you to add user-defined routing rules, and targets is used to configure some output targets, as follows:
Let's temporarily remove the description code of the comment and restore it to the most concise XML format, as follows:
Quick use
1. Create a Nlog instance
Private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger ()
two。 Use Debug for output
Since we have added the final output target of Debug in Nlog.Config, we try to output the contents of Debug:
Class Program {private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger (); static void Main (string [] args) {Logger.Debug ("I had an accident!") ; Console.ReadKey ();}}
After running, you can generate a logs folder in the current output directory and generate a log file for the current calculator date (this rule can be configured in Nolog.Config), as shown in the figure:
Explain the configuration in detail
With regard to rules, we can add multiple routing rules and point to multiple destinations, as follows:
Add support for Console output
1. Add Info to rules, and writeTo points to a target of Console
two。 Use Info output
Export to CSV fil
1. Add a new route to rules to support the csv file, and add a target to output to the csv file, where column can be used to specify the format of the data content generated by each column
Finally, Debug ("xxxx") is called, and the output is as follows:
Configure log size
Nlog allows users to configure the size of a single file. After the content is too long and slow, Nlog automatically creates a new copy of the file and inserts a new log output.
MaxArchiveFiles: maximum number of copies of files allowed to be generated
ArchiveAboveSize: allows the maximum capacity of a single file
ArchiveEvery: generate by day
Layout: the current content layout format
FileName: contains the path and file name of the complete generated file
Configure log rating
A single file target can be used to write more than one file at a time. The following configuration will cause log entries at each log level to be written to a separate file that supports the following formats:
Trace.log
Debug.log
Info.log
Warn.log
Error.log
Fatal.log
You only need to configure the following in Nlog.Config:
Configure build rules
You only need to modify the fixed name program ${shortdate} written in filename.
Log filter
You can configure a custom log filter fliter for each route, as shown below, which demonstrates the use of various expressions to configure the filter:
Conditional language
Filter expressions are written in a special mini language. The language includes:
Relational operator: =,! =
Note: some predefined XML characters may need to be escaped. For example, if you try to use'
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.