C#-NLog记录日志

 

Nuget获取包

C#-NLog记录日志_第1张图片

 

 

 

 

配置文件

 

xml version="1.0" encoding="utf-8" ?>
<nlog xmlns=
"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"> <variable name="myvar" value="myvalue"/> 
  
 <targets> 
 
   <target xsi:type="File" name="f" fileName=
"D:/${shortdate}.log" layout="${longdate} 
[${level:uppercase=true}] ${message}" /> 
    
   <target name="debugger" 
xsi:type="Debugger" layout="${longdate} [${level:uppercase=true}] ${message}"/> 
 
   
<target name="console" xsi:type="Console" layout="${longdate} 
[${level:uppercase=true}] ${message}"/> 

targets> 
  
 <rules> 
 <logger name="*" 
minlevel="Trace" writeTo="f" /> <logger name="*" writeTo="debugger"/> <logger 
name="*" writeTo="console"/> 
rules> 


nlog> 

 

 

调用范例

            try
            {
                int a = 1;
                int b = 0;
                int c = a / b;

            }
            catch(Exception ex)
            {
                Logger _logger = LogManager.GetCurrentClassLogger(); 
                //_logger.Trace("Trace Message"); 
                //_logger.Debug("DebugMessage"); 
                //_logger.Info("InfoMessage");
                _logger.Error(ex.Message+ex.StackTrace);
                //_logger.Fatal("FatalMessage");
            }

 

 

你可能感兴趣的:(C#-NLog记录日志)