Log framework NLog, log framework nlog
Here we will compare it with other products according to the old rules:
Currently, there are two old log frameworks on the. net platform: Log4net and NLog.
We will compare the two frameworks.
Log4net
Log4net is a classic log framework that can output logs to the console, files, databases, system events, and emails. It allows the log system to take effect in real time through configuration. For example, you do not need to restart the program when you modify the configuration to change the output target or log level during service running. The disadvantage is that the configuration is cumbersome. I cannot configure the configuration if I do not check the information.
NLog
NLog is a log framework similar to Log4net and is a new log framework. We will compare their support and performance.
Learn from the testing results of the bloggers
I almost sent a letter. Later, the author sent the following content in September 1, 2017.
The above performance tests are no longer effective. According to feedback from netizens, the results of the above performance tests may be unfair due to slight differences in the output content. Therefore, log4Net may have more computations for output. When optimizing the test code, only the log framework prints the log content. The rest of the time, log level, and log class names are not printed. The latest Dll version is used, and the performance of the two frameworks is almost the same.
After all, it is an old-brand log framework, and it is still lost to new users.
However, in terms of the update speed, Nlog has been continuously updated, and more are supported. So let's talk about it here.
First load NLog with nuget
After loading the file, let's see if there are any more Trojans in the project?
Log configuration (excerpt)
By scanning common directories at startup, NLog will try to use the found configuration information for automatic self-configuration. When you run an independent *. exe client executable program, NLog searches for configuration information in the following directory:
Standard Program configuration file (usuallyProgram name .exe. config)
Under the Program directoryProgram name .exe. nlogFile
Under the Program directoryNLog. configFile
Under the directory where NLog. dll is locatedNLog. dll. nlogFile
If the NLOG_GLOBAL_CONFIG_FILE environment variable is defined, the file to which the variable points
If it is an ASP. NET program, the searched directories include:
Standard web program configuration fileWeb. config
AndWeb. configIn the same directoryWeb. nlogFile
Under the Program directoryNLog. configFile
Under the directory where NLog. dll is locatedNLog. dll. nlogFile
If the NLOG_GLOBAL_CONFIG_FILE environment variable is defined, the file to which the variable points
Because. NET Compact Framework does not support program configuration files (*. exe. config) and environment variables, NLog will only scan these areas:
Under the Program directoryNLog. configFile
Under the directory where NLog. dll is locatedNLog. dll. nlogFile
If the NLOG_GLOBAL_CONFIG_FILE environment variable is defined, the file to which the variable points
NLog configuration file NLog. config. With the above content, we can know that NLog can be configured in web. config or that the independent NLog. config file is OK.
Web. config
<Configuration>
<ConfigSections>
<Sectionname = "nlog" type = "NLog. Config. ConfigSectionHandler, NLog"/>
</ConfigSections>
<Nlog>
This part is the content in NLog. config.
</Nlog>
</Configuration>
Nlog. config
<? Xmlversion = "1.0" encoding = "UTF-8"?>
<Nlogxmlns = "http://www.nlog-project.org/schemas/NLog.xsd"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://www.nlog-project.org/schemas/NLog.xsd NLog. xsd"
AutoReload = "true"
ThrowExceptions = "false"
InternalLogLevel = "Off" internalLogFile = "c: \ temp \ nlog-internal.log">
<! -- Optional, add some variables
Https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<Variablename = "myvar" value = "myvalue"/>
<! --
See https://github.com/nlog/nlog/wiki/Configuration-file
For information on customizing logging rules and outputs.
-->
<Targets>
<! --
Add your targets here
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
-->
<! --
Write events to a file with the date in the filename.
<Target xsi: type = "File" name = "f" fileName = "$ {basedir}/logs/$ {daily date}. log"
Layout = "$ {longdate }$ {uppercase: $ {level }}$ {message}"/>
-->
</Targets>
<Rules>
<! -- Add your logging rules here -->
<! --
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
<Logger name = "*" minlevel = "Debug" writeTo = "f"/>
-->
</Rules>
</Nlog>
We will explain the above node names:
- Name-Target name
- Type-target type-such as "File", "Database", and "Mail ". If you use a namespace, this attribute will be named xsi: type.