NLog 记录日志是微软官方推荐使用。

接下来,通过配置日志记录到文件和Sql Server数据库。

第一步:首先添加包NLog.Config (可通过微软添加包命令Install-Package 包名进行添加,也可以通过管理NuGet程序包进行添加),添加成功后会生成NLog.config配置文件。并对该配置文件进行配置。详细配置可参考Git上 NLog说明。

一下是我个人配置。

 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       xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
 5       autoReload="true"
 6       throwExceptions="false"
 7       internalLogLevel="Warn"   
 8       internalLogFile="Logs/nlog-internal.log">
 9   
10   <!--internalLogLevel="Off"-->
11   <!-- optional, add some variables
12   https://github.com/nlog/NLog/wiki/Configuration-file#variables
13   -->
14   <variable name="myvar" value="myvalue"/>
15 
16   <!--
17   See https://github.com/nlog/nlog/wiki/Configuration-file
18   for information on customizing logging rules and outputs.
19    -->
20   <targets>
21 
22     <!--
23     add your targets here
24     See https://github.com/nlog/NLog/wiki/Targets for possible targets.
25     See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
26     -->
27 
28     <!--
29     Write events to a file with the date in the filename.
30     <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
31             layout="${longdate} ${uppercase:${level}} ${message}" />
32     -->
33 
34     <!-- write logs to file -->
35     <target xsi:type="File" name="allfile" fileName="Logs/${date:format=yyyyMM}/nlog-all-${shortdate}.log"
36              layout="${longdate}|${event-properties:item=EventId.Id}|${uppercase:${level}}|${logger} ${newline}${message} ${exception} ${newline}" />
37 
38     <target xsi:type="File" name="ownFile-web" fileName="Logs/${date:format=yyyyMM}/nlog-own-${shortdate}.log"
39              layout="${longdate}|${event-properties:item=EventId.Id}|${uppercase:${level}}|${logger} ${newline}${message} ${exception} ${newline} --- |url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
40 
41     <target xsi:type="Null" name="blackhole" />
42 
43     <target xsi:type="Database" name="database">
44       <connectionString>${var:connectionString}</connectionString>
45       <commandText>
46         insert into syslogs (Application,Levels,Operatingtime,Operatingaddress,Userid,Logger,Callsite,Requesturl,Referrerurl,Action,Message,Exception)
47         values (@application,@levels,@operatingtime,@operatingaddress,@userid,@logger,@callSite,@requesturl,@referrerurl,@action,@message,@exception);
48       </commandText>
49       <parameter name="@application" layout="WebApi" />
50       <parameter name="@levels" layout="${level}" />
51       <parameter name="@operatingTime" layout="${date}" />
52       <parameter name="@operatingaddress" layout="${aspnet-Request-IP}" />
53       <parameter name="@userid" layout="1" />
54       <parameter name="@logger" layout="${logger}" />
55       <parameter name="@callSite" layout="${callsite}" />
56       <parameter name="@requesturl" layout="${aspnet-request-url}" />
57       <parameter name="@referrerurl" layout="${aspnet-request}" />
58       <parameter name="@action" layout="${aspnet-mvc-action}" />
59       <parameter name="@message" layout="${message}" />
60       <parameter name="@exception" layout="${exception:tostring}" />
61     </target>
62     
63   </targets>
64 
65   <rules>
66     <!-- add your logging rules here -->
67 
68     <!--
69     Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace)  to "f"
70     <logger name="*" minlevel="Debug" writeTo="f" />
71     -->
72 
73     <!--All logs, including from Microsoft-->
74     <!--minlevel 改为Trace 跟踪全部 Error 只捕获异常-->
75     <logger name="*" minlevel="Error" writeTo="allfile" />
76 
77     <!--Skip Microsoft logs and so log only own logs-->
78     <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
79     <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
80     <logger name="*" minlevel="Trace" writeTo="database" />    
81     
82   </rules>
83 </nlog>
84 
85 
86 <!--增加引用
87 <PackageReference Include="NLog.Extensions.Logging" Version="1.2.1" />
88 <PackageReference Include="NLog.Web.AspNetCore" Version="4.6.0" />-->
NLog.config

相关文章:

  • 2021-12-20
  • 2022-12-23
  • 2022-01-01
  • 2022-12-23
  • 2021-10-22
  • 2022-02-28
猜你喜欢
  • 2021-05-31
  • 2022-02-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-19
  • 2022-02-12
  • 2021-06-04
相关资源
相似解决方案