[VS] [C#] 替项目加入 log4net 吧~

时间:2022-02-09 01:57:23

摘要:[VS] [C#] 替项目加入 log4net 吧~


最近刚接触到好用的 log4net,

在写 Window Service 时,

当然不免俗的也来加入使用,

网络上的文章数据很多,

我这篇也是参照别人的数据一步一步做下来的,

就留下来做个记录吧~

先从好用的 NuGet 套件中, 找到 log4net 安装包, 安装下来

这时候, 如果大家和我一样写的是 Application 的话, 应该会出现一个 App.config吧,

和大家分享一下我的 App.config, 有需要的人可以直接COPY

===























===

最后, 再修改一下AssemblyInfo.cs, 就可以印出来噜

好吧, 这时候就来开心的使用吧

目录下会出现一个 txt 档

打开后会有一些内容

不喜欢我的设定吗? 没关系, 有文档可以查询相关的设定,

Conversion Pattern Name Effect
a   Equivalent to?appdomain  
appdomain   Used to output the friendly name of the AppDomain where the logging event was generated.  
aspnet-cache  

Used to output all cache items in the case of?%aspnet-cache?or just one named item if used as?%aspnet-cache{key}

This pattern is not available for Compact Framework or Client Profile assemblies.

 
aspnet-context  

Used to output all context items in the case of?%aspnet-context?or just one named item if used as?%aspnet-context{key}

This pattern is not available for Compact Framework or Client Profile assemblies.

 
aspnet-request  

Used to output all request parameters in the case of?%aspnet-request?or just one named param if used as?%aspnet-request{key}

This pattern is not available for Compact Framework or Client Profile assemblies.

 
aspnet-session  

Used to output all session items in the case of?%aspnet-session?or just one named item if used as?%aspnet-session{key}

This pattern is not available for Compact Framework or Client Profile assemblies.

 
c   Equivalent to?logger  
C   Equivalent to?type  
class   Equivalent to?type  
d   Equivalent to?date  
date  

Used to output the date of the logging event in the local time zone. To output the date in universal time use the?%utcdate?pattern. The date conversion specifier may be followed by adate format specifier?enclosed between braces. For example,?%date{HH:mm:ss,fff}?or%date{dd MMM yyyy HH:mm:ss,fff}. If no date format specifier is given then ISO8601 format is assumed (Iso8601DateFormatter).

The date format specifier admits the same syntax as the time pattern string of the?ToString.

For better results it is recommended to use the log4net date formatters. These can be specified using one of the strings "ABSOLUTE", "DATE" and "ISO8601" for specifyingAbsoluteTimeDateFormatter,?DateTimeDateFormatter?and respectivelyIso8601DateFormatter. For example,?%date{ISO8601}?or?%date{ABSOLUTE}.

These dedicated date formatters perform significantly better than?ToString.

 
exception  

Used to output the exception passed in with the log message.

If an exception object is stored in the logging event it will be rendered into the pattern output with a trailing newline. If there is no exception then nothing will be output and no trailing newline will be appended. It is typical to put a newline before the exception and to have the exception as the last data in the pattern.

 
F   Equivalent to?file  
file  

Used to output the file name where the logging request was issued.

WARNING?Generating caller location information is extremely slow. Its use should be avoided unless execution speed is not an issue.

See the note below on the availability of caller location information.

 
identity  

Used to output the user name for the currently active user (Principal.Identity.Name).