整体架构目录:ASP.NET Core分布式项目实战-目录
一、介绍
此篇文章将会介绍项目的全局异常收集以及采用log4net或者NLog记录。
众所周知,一旦自己的项目报错,如果没有进行处理都是显示不友好的,有得甚至直接爆出错误页面,看的也是很奇怪。
为了避免出现这样的错误以及在错误出现的时候可以进行收集错误,供维护人员进行bug修改,因此需要进行全局异常的收集。
让我们开始部署吧。
此篇文章的目录
1、log4net使用
2、Nlog使用
后期将会把NLog+ELK进行结合部署收集我们的asp.net core的项目。大家可以拭目以待吧。
二、部署(log4net使用)
1、新建一个asp.net core webapi的项目
然后目前我先引入 log4net nuget包。
2、然后创建一个log4net.config文件
此文件中,我创建了一个是记录 错误的文件夹(LogError)以及是记录操作的文件夹(LogInfo),代码如下:我把需要记录的文件放在了log文件夹下面。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<log4net>
<!-- 错误日志类-->
<logger name=<span style="color: #800000;">"</span><span style="color: #800000;">logerror</span><span style="color: #800000;">"</span>>
<level value=<span style="color: #800000;">"</span><span style="color: #800000;">ALL</span><span style="color: #800000;">"</span> />
<appender-<span style="color: #0000ff;">ref</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">ErrorAppender</span><span style="color: #800000;">"</span> />
</logger>
<!-- 错误日志附加介质-->
<appender name=<span style="color: #800000;">"</span><span style="color: #800000;">ErrorAppender</span><span style="color: #800000;">"</span> type=<span style="color: #800000;">"</span><span style="color: #800000;">log4net.Appender.RollingFileAppender</span><span style="color: #800000;">"</span>>
<!--日志文件路径-->
<param name=<span style="color: #800000;">"</span><span style="color: #800000;">File</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><strong><span style="color: #ff0000;">Log\\LogError\\</span></strong><span style="color: #800000;">"</span> />
<!--是否是向文件中追加日志-->
<param name=<span style="color: #800000;">"</span><span style="color: #800000;">AppendToFile</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span> />
<!--log保留天数-->
<param name=<span style="color: #800000;">"</span><span style="color: #800000;">MaxSizeRollBackups</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">1000</span><span style="color: #800000;">"</span> />
<!--最大文件大小-->
<param name=<span style="color: #800000;">"</span><span style="color: #800000;">MaxFileSize</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">10240</span><span style="color: #800000;">"</span> />
<!--日志文件名是否是固定不变的-->
<param name=<span style="color: #800000;">"</span><span style="color: #800000;">StaticLogFileName</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">false</span><span style="color: #800000;">"</span> />
<!--日志文件名格式为:<span style="color: #800080;">2008</span>-<span style="color: #800080;">08</span>-<span style="color: #800080;">31</span>.log-->
<param name=<span style="color: #800000;">"</span><span style="color: #800000;">DatePattern</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">yyyy-MM-dd&quot;.htm&quot;</span><span style="color: #800000;">"</span> />
<!--日志根据日期滚动-->
<param name=<span style="color: #800000;">"</span><span style="color: #800000;">RollingStyle</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">Date</span><span style="color: #800000;">"</span> />
<!--信息日志布局-->
<layout type=<span style="color: #800000;">"</span><span style="color: #800000;">log4net.Layout.PatternLayout</span><span style="color: #800000;">"</span>>
<param name=<span style="color: #800000;">"</span><span style="color: #800000;">ConversionPattern</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">&lt;HR COLOR=red&gt;%n【异常时间】:%d [%t] &lt;BR&gt;%n【异常级别】:%-5p &lt;BR&gt;%n%m &lt;BR&gt;%n &lt;HR Size=1&gt;</span><span style="color: #800000;">"</span> />
</layout>
</appender>
<!-- 信息日志类 -->
<logger name=<span style="color: #800000;">"</span><span style="color: #800000;">loginfo</span><span style="color: #800000;">"</span>>
<level value=<span style="color: #800000;">"</span><span style="color: #800000;">ALL</span><span style="color: #800000;">"</span> />
<appender-<span style="color: #0000ff;">ref</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">InfoAppender</span><span style="color: #800000;">"</span> />
</logger>
<!-- 信息日志附加介质-->
<appender name=<span style="color: #800000;">"</span><span style="color: #800000;">InfoAppender</span><span style="color: #800000;">"</span> type=<span style="color: #800000;">"</span><span style="color: #800000;">log4net.Appender.RollingFileAppender</span><span style="color: #800000;">"</span>>
<!--日志文件路径-->
<param name=<span style="color: #800000;">"</span><span style="color: #800000;">File</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><strong><span style="color: #ff0000;">Log\\LogInfo\\</span></strong><span style="color: #800000;">"</span> />
<!--是否是向文件中追加日志-->
<param name=<span style="color: #800000;">"</span><span style="color: #800000;">AppendToFile</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span> />
<!--log保留天数-->
<param name=<span style="color: #800000;">"</span><span style="color: #800000;">MaxSizeRollBackups</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">100</span><span style="color: #800000;">"</span> />
<param name=<span style="color: #800000;">"</span><span style="color: #800000;">MaxFileSize</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">1</span><span style="color: #800000;">"</span> />
<!--日志文件名是否是固定不变的-->
<param name=<span style="color: #800000;">"</span><span style="color: #800000;">StaticLogFileName</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">false</span><span style="color: #800000;">"</span> />
<!--日志文件名格式为:<span style="color: #800080;">2008</span>-<span style="color: #800080;">08</span>-<span style="color: #800080;">31</span>.log-->
<param name=<span style="color: #800000;">"</span><span style="color: #800000;">DatePattern</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">yyyy-MM-dd&quot;.htm&quot;</span><span style="color: #800000;">"</span> />
<!--日志根据日期滚动-->
<param name=<span style="color: #800000;">"</span><span style="color: #800000;">RollingStyle</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">Date</span><span style="color: #800000;">"</span> />
<!--信息日志布局-->
<layout type=<span style="color: #800000;">"</span><span style="color: #800000;">log4net.Layout.PatternLayout</span><span style="color: #800000;">"</span>>
<param name=<span style="color: #800000;">"</span><span style="color: #800000;">ConversionPattern</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">&lt;HR COLOR=blue&gt;%n日志时间:%d [%t] &lt;BR&gt;%n日志级别:%-5p &lt;BR&gt;%n%m &lt;BR&gt;%n &lt;HR Size=1&gt;</span><span style="color: #800000;">"</span> />
</layout>
</appender>
</log4net>
<!-- To customize the asp.net core module uncomment and edit the following section.
For more info see https: style="color: #008000;">//go.microsoft.com/fwlink/?linkid=838655 -->
<!--
<system.webServer>
<handlers>
<remove name=<span style="color: #800000;">"</span><span style="color: #800000;">aspNetCore</span><span style="color: #800000;">"</span>/>
<add name=<span style="color: #800000;">"</span><span style="color: #800000;">aspNetCore</span><span style="color: #800000;">"</span> path=<span style="color: #800000;">"</span><span style="color: #800000;">*</span><span style="color: #800000;">"</span> verb=<span style="color: #800000;">"</span><span style="color: #800000;">*</span><span style="color: #800000;">"</span> modules=<span style="color: #800000;">"</span><span style="color: #800000;">AspNetCoreModule</span><span style="color: #800000;">"</span> resourceType=<span style="color: #800000;">"</span><span style="color: #800000;">Unspecified</span><span style="color: #800000;">"</span>/>
</handlers>
<aspNetCore processPath=<span style="color: #800000;">"</span><span style="color: #800000;">%LAUNCHER_PATH%</span><span style="color: #800000;">"</span> arguments=<span style="color: #800000;">"</span><span style="color: #800000;">%LAUNCHER_ARGS%</span><span style="color: #800000;">"</span> stdoutLogEnabled=<span style="color: #800000;">"</span><span style="color: #800000;">false</span><span style="color: #800000;">"</span> stdoutLogFile=<span style="color: #800000;">"</span><span style="color: #800000;">.\logs\stdout</span><span style="color: #800000;">"</span> />
</system.webServer>
-->
</configuration>