1、通过nuget 查找 下载 NLog.Extensions.Logging
2、配置nlog.config文件
1 <?xml version="1.0" encoding="utf-8" ?> 2 <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 throwExceptions="true" internalLogFile="c: log.txt" internalLogLevel="Debug"> 5 6 <!-- 7 See http://nlog-project.org/wiki/Configuration_file 8 for information on customizing logging rules and outputs. 9 --> 10 <!--<nlog throwExceptions="true" internalLogFile="c: log.txt" internalLogLevel="Debug" />--> 11 <targets> 12 <!-- add your targets here --> 13 <target xsi:type="File" name="file" fileName="${basedir}/logs/${level}/${shortdate}.log" 14 layout="${longdate} ${uppercase:${level}} ${message}" /> 15 16 <!--<target type="Database" name="database" connectionString="Data Source=localhost;Initial Catalog=Test;User ID=sa;Password=sa;MultipleActiveResultSets=true; "> 17 <commandText> 18 insert into ErrorLog ([CreateDate], [Origin], [LogLevel], [Message], [StackTrace]) values (@createDate, @origin, @logLevel, @message, @stackTrace); 19 </commandText> 20 <parameter name="@createDate" layout="${longdate}"/> 21 日志发生时间 22 <parameter name="@origin" layout="${callsite}"/> 23 日志发生时间 24 <parameter name="@logLevel" layout="${level}"/> 25 日志等级 26 <parameter name="@message" layout="${message}"/> 27 日志信息 28 <parameter name="@stackTrace" layout="${stacktrace}"/> 29 日志发生时间 30 </target>--> 31 <!-- 32 <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log" 33 layout="${longdate} ${uppercase:${level}} ${message}" /> 34 --> 35 </targets> 36 37 <rules> 38 <!-- add your logging rules here --> 39 <logger name="*" minlevel="Trace" writeTo="file"/> 40 <!--<logger name="*" level="Error" appendTo="database"/>--> 41 42 43 </rules> 44 </nlog>
3、在Configure 添加配置
//日志 loggerFactory.AddNLog(); env.ConfigureNLog("Config/nlog.config");
4、往project.json 添加配置 发布输出
1 "publishOptions": { 2 "include": [ 3 "wwwroot", 4 "**/*.cshtml", 5 "appsettings.json", 6 "web.config", 7 "Config", //nlog 8 "Export" //Export 9 ] 10 },
5、封装的操作类
1 /// <summary> 2 /// 日志 3 /// </summary> 4 public class NLogger 5 { 6 private static readonly Logger logger = LogManager.GetCurrentClassLogger(); 7 8 #region "等级1-Debug" 9 /// <summary> 10 /// 记录调试信息 11 /// </summary> 12 /// <param name="userInfo">用户信息</param> 13 /// <param name="msg">具体信息</param> 14 public static void Debug(string msg, string userInfo = "") 15 { 16 string info = string.Empty; 17 if (userInfo != "") 18 { 19 info = userInfo; 20 } 21 info = info + " " + msg; 22 23 logger.Debug(info); 24 } 25 26 #endregion 27 28 #region "等级2-Info" 29 30 31 /// <summary> 32 /// 记录信息 33 /// </summary> 34 /// <param name="userInfo">用户信息</param> 35 /// <param name="msg">具体信息</param> 36 public static void Info(string msg, string userInfo = "") 37 { 38 string info = string.Empty; 39 if (userInfo != "") 40 { 41 info = userInfo; 42 } 43 info = info + " " + msg; 44 logger.Info(info); 45 46 } 47 48 #endregion 49 50 #region "等级3-Warn" 51 52 53 54 /// <summary> 55 /// 记录警告信息 56 /// </summary> 57 /// <param name="userInfo">用户信息</param> 58 /// <param name="msg">具体信息</param> 59 public static void Warn(string msg, string userInfo = "") 60 { 61 string info = string.Empty; 62 if (userInfo != "") 63 { 64 info = userInfo; 65 } 66 info = info + " " + msg; 67 logger.Warn(info); 68 69 } 70 71 #endregion 72 73 #region "等级4-Error" 74 75 76 /// <summary> 77 /// 记录错误信息 78 /// </summary> 79 /// <param name="userInfo">用户信息</param> 80 /// <param name="msg">具体信息</param> 81 public static void Error(string msg, string userInfo = "") 82 { 83 string info = string.Empty; 84 if (userInfo != "") 85 { 86 info = userInfo; 87 } 88 info = info + " " + msg; 89 logger.Error(info); 90 91 } 92 93 94 95 /// <summary> 96 /// 记录异常信息 97 /// </summary> 98 /// <param name="userInfo">用户信息</param> 99 /// <param name="ex">具体异常</param> 100 public static void Error(Exception ex, string userInfo = "") 101 { 102 string info = ""; 103 if (userInfo != "") 104 { 105 info = userInfo; 106 } 107 info = info+" " + ErrorDetails(ex); 108 logger.Error(info); 109 110 } 111 112 /// <summary> 113 /// 将异常转成字符串 114 /// </summary> 115 /// <param name="ex"></param> 116 /// <returns></returns> 117 private static string ErrorDetails(Exception ex) 118 { 119 StringBuilder sb = new StringBuilder(); 120 int count = 0; 121 string appString = ""; 122 while (ex != null) 123 { 124 if (count > 0) 125 { 126 appString += " "; 127 } 128 sb.AppendLine(appString + " <br>异常消息:" + ex.Message); 129 sb.AppendLine(appString + " <br>异常类型:" + ex.GetType().FullName); 130 // sb.AppendLine(appString + " <br>异常方法:" + (ex.TargetSite == null ? null : ex.TargetSite.Name)); 131 sb.AppendLine(appString + " <br>异常源:" + ex.Source); 132 if (ex.StackTrace != null) 133 { 134 sb.AppendLine(appString + "<br>异常堆栈:" + ex.StackTrace); 135 } 136 if (ex.InnerException != null) 137 { 138 sb.AppendLine(appString + "<br>内部异常:"); 139 count++; 140 } 141 ex = ex.InnerException; 142 } 143 144 return sb.ToString().Replace("位置:", "<br>位置"); 145 } 146 147 #endregion 148 149 #region "等级5-Fatal " 150 151 152 153 /// <summary> 154 /// 记录致命错误 155 /// </summary> 156 /// <param name="userInfo">用户信息</param> 157 /// <param name="msg">具体信息</param> 158 public static void Fatal(string msg, string userInfo = "") 159 { 160 string info = string.Empty; 161 if (userInfo != "") 162 { 163 info = userInfo; 164 } 165 info = info + " " + msg; 166 logger.Fatal(info); 167 168 } 169 170 #endregion 171 }
5、调用
NLogger.Debug("测试");