C # Nlog Experience

Source: Internet
Author: User

I get the Nlog Log class library support directly in NuGet and re-complete the nuget knowledge (need to be in the foreground of networking)

①: Right-click on the project-manage NuGet packages (as shown)

650) this.width=650; "title=" 01.png "alt=" wkiom1jbex6gxnbfaada8jchpkw886.png-wh_50 "src=" https://s5.51cto.com/ Wyfs02/m00/8e/76/wkiom1jbex6gxnbfaada8jchpkw886.png-wh_500x0-wm_3-wmp_4-s_1226922436.png "/>

②: Appears in the manager panel click on the left "network" also sign-"in the search bar on the right, enter" NLog "carriage return-" in the middle Select "NLog" column click Install (as shown)

650) this.width=650; "title=" 02.png "alt=" wkiom1jbfa2btyeqaag8djmubxu996.png-wh_50 "src=" https://s1.51cto.com/ Wyfs02/m01/8e/77/wkiom1jbfa2btyeqaag8djmubxu996.png-wh_500x0-wm_3-wmp_4-s_227092821.png "/>

Wait for the download (so it needs to be networked)

650) this.width=650; "title=" 03.png "alt=" wkiom1jbfkzytt19aacwrtmbysw053.png-wh_50 "src=" https://s5.51cto.com/ Wyfs02/m02/8e/77/wkiom1jbfkzytt19aacwrtmbysw053.png-wh_500x0-wm_3-wmp_4-s_423317853.png "/>

650) this.width=650; "title=" 04.png "alt=" wkiom1jbgb-c-damaaccaxkeks8007.png-wh_50 "src=" https://s5.51cto.com/ Wyfs02/m01/8e/77/wkiom1jbgb-c-damaaccaxkeks8007.png-wh_500x0-wm_3-wmp_4-s_2725644158.png "/>

There is an episode here, because my console program does not have a configuration file (App. config), which is generally the new console program. Do not say, do add, but must note: Only one configuration file can be added (action)

650) this.width=650; "title=" 05.png "alt=" wkiom1jbmxnd2w4jaad70d1c350299.png-wh_50 "src=" https://s5.51cto.com/ Wyfs02/m00/8e/78/wkiom1jbmxnd2w4jaad70d1c350299.png-wh_500x0-wm_3-wmp_4-s_4285669577.png "/>

It is difficult to find a person

650) this.width=650; "title=" 06.png "alt=" wkiol1jbm6qhwgucaabduetfsag911.png-wh_50 "src=" https://s2.51cto.com/ Wyfs02/m00/8e/76/wkiol1jbm6qhwgucaabduetfsag911.png-wh_500x0-wm_3-wmp_4-s_1308138662.png "/>

Attach the configuration of the entire app. Config

<?xml version= "1.0"  encoding= "Utf-8"  ?><configuration>  < Configsections>    <section name= "Nlog"  type= " Nlog.config.configsectionhandler, nlog "/>  </configsections>  <nlog  Autoreload= "true"  internalloglevel= "Trace"  internallogfile= "Logs/internallog.txt" >     <targets>      <target name= "T1"  type= "File"  filename= "${basedir}/logs/${shortdate}.log"                layout= "${longdate} ${callsite} ${level}:                ${message} ${event-context:item=exception}  ${stacktrace} ${event-context:item=stacktrace} "/>      <target  name= "T2"  type= "Console"  layout= "${date:format=yyyymmDdhhmmss} ${callsite} ${level} ${message} "/>    </targets>     <rules>      <logger name= "File"  minlevel= " Debug " maxlevel=" Off " writeto=" T1 "/>      <logger name=" Console " minlevel=" Trace " maxlevel=" Off " writeto=" T2 "/>    </rules >  </nlog>  <startup>    <supportedRuntime  version= "v4.0"  sku= ". netframework,version=v4.5 " />  </startup></configuration>

Annotations:

①, autoreload= "true" means that if the configuration file is modified without restarting the application, Nlog will automatically load the app;

Internalloglevel= "Trace" internallogfile= "Logs/internallog.txt" This setting can write Nlog internal log messages to the InternalLog.txt file in the Logs folder in the application directory (this configuration is often used to debug nlog configuration is correct, after debugging, it is best to shut down to improve performance)
<target> configuration: type= "file| Console property is the set log output target is "file" or "console" (console);


②,type= "File" When you want to specify the FileName property, filename= "${basedir}/logs/${shortdate}.log" to set the path and name of the journal record file, That is, the log directory under the application is in yyyy-mm-dd.log format;

Layout= "${date:format=yyyymmddhhmmss} ${callsite} ${level} ${message}" to set the log output format (see the official website for instructions).


③,name= "file" means that the configured rules apply to the logger name called "File", and if it is filled, then all logger apply this rule.
Minlevel= "Debug" maxlevel= "Off" is used to configure logging with a minimum level of "debug" to "Off" (note: This can also be set with levels= "Debug,off", Description only output debug level and off level of the log, the official website here is explained in error:)
Writeto= "T1" where t1,t2 represents the target output of the targets named T1 and T2, respectively, which indicates that the log information will be output to the file/console, respectively.


It's necessary to say the message level

Unzip the downloaded compressed package: http://nlog-project.org/Please download it from official website

After decompression in the Src/nlog there is a LogLevel.cs file, there is message level information (can open to see, a total of 6 levels, from low to high)

650) this.width=650; "title=" 07.png "alt=" wkiol1jbqiwgs3xdaahehzooroi542.png-wh_50 "src=" https://s2.51cto.com/ Wyfs02/m00/8e/76/wkiol1jbqiwgs3xdaahehzooroi542.png-wh_500x0-wm_3-wmp_4-s_944935496.png "/>


Well, the code test:

Using system;using system.collections.generic;using system.linq;using system.text;using  NLog;namespace MyNLog{    public class Program     {        private static Logger  @loggerFile  =  logmanager.getlogger ("file");        private static  logger  @loggerConsole  = logmanager.getlogger ("console");         static void main (String[] args)         {             loggerfile.debug ("A File  Test message = debug ");             Loggerfile.warn ("A file test message = warn");            &nBsp;loggerfile.trace ("A file test message = trace");             loggerconsole.debug ("a console test message =  debug ");             console.read ();         }    }}

Analytical:

①, @loggerFile class is the one that writes logs to a file.

②, @loggerConsole class is writing messages to the console.

650) this.width=650; "title=" 08.png "alt=" wkiol1jbs17qxmnraaagbr6o16e587.png-wh_50 "src=" https://s3.51cto.com/ Wyfs02/m01/8e/77/wkiol1jbs17qxmnraaagbr6o16e587.png-wh_500x0-wm_3-wmp_4-s_3073700916.png "/>

As for the log in the Debug/logs

650) this.width=650; "title=" 09.png "alt=" wkiol1jbs8hsywukaabezbtz16a801.png-wh_50 "src=" https://s4.51cto.com/ Wyfs02/m00/8e/77/wkiol1jbs8hsywukaabezbtz16a801.png-wh_500x0-wm_3-wmp_4-s_2366444433.png "/>

Because of configuration issues, see App. Config, 2017-03-09.log does not have a File test Message = Trace

650) this.width=650; "title=" 10.png "alt=" wkiol1jbtevylywmaabpwp3rjwm111.png-wh_50 "src=" https://s2.51cto.com/ Wyfs02/m02/8e/77/wkiol1jbtevylywmaabpwp3rjwm111.png-wh_500x0-wm_3-wmp_4-s_4017402145.png "/>


This article is from the "Better_power_wisdom" blog, make sure to keep this source http://aonaufly.blog.51cto.com/3554853/1904926

C # Nlog Experience

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.