我有以下NLog配置文件
<?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">
<!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue"/>
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<target
name="logfile"
xsi:type="File"
fileName="log.txt"
archiveFileName="log.{#}.txt"
archiveNumbering="DateAndSequence"
archiveAboveSize="1000000"
archiveDateFormat="yyyyMMdd"
/>
<target
name="byteArrayLogfile"
xsi:type="File"
fileName="log.ba.txt"
archiveFileName="log.ba.{#}.txt"
archiveNumbering="DateAndSequence"
archiveAboveSize="1000000"
archiveDateFormat="yyyyMMdd"
/>
<target
name="console"
xsi:type="Console" />
</targets>
<rules>
<logger name="StackTraceLogger" minlevel="Warn" maxlevel="Error" writeTo="logfile" layout="${longdate}|${threadid}|${callsite-linenumber:skipFrames=Integer}|${level:uppercase=true}|${message}${newline}${exception:format=tostring}" final="true"/>
<logger name="ByteArrayLogger" minlevel="Debug" writeTo="byteArrayLogfile" layout="${longdate}|${threadid}|${level:uppercase=true}${newline}${message}" final="true"/>
<logger name="Ctrack.DMT.*" minlevel="Debug" writeTo="logfile" layout="${longdate}|${threadid}|${callsite-linenumber:skipFrames=Integer}|${level:uppercase=true}|${logger}|${message}" />
<logger name="Ctrack.DMT.*" minlevel="Trace" writeTo="console" layout="${longdate}|${threadid}|${callsite-linenumber:skipFrames=Integer}|${level:uppercase=true}|${logger}|${message}" />
<logger name="ServerForm.*" minlevel="Debug" writeTo="logfile" layout="${longdate}|${threadid}|${callsite-linenumber:skipFrames=Integer}|${level:uppercase=true}|${logger}|${message}" />
<logger name="ServerForm.*" minlevel="Trace" writeTo="console" layout="${longdate}|${threadid}|${callsite-linenumber:skipFrames=Integer}|${level:uppercase=true}|${logger}|${message}" />
</rules>
</nlog>
目的是:
每个班级都有以下顶部:
private static Logger logger = LogManager.GetCurrentClassLogger();
private static Logger loggerEL = LogManager.GetLogger("ExceptionLogger");
private static Logger loggerBA = LogManager.GetLogger("ByteArrayLogger");
记录异常的步骤
string message = "Exception while trying to save " + Name + " to the database.";
logger.Error("{0}|{1}", MethodName, message);
loggerEL.Error(ex, "{0}|{1}", MethodName, message);
Stacktraces可能会在不久的将来被移出到一个单独的文件
不起作用的事
此外,Intellisense突出显示布局并将The 'layout' attribute is not declared
视为警告。
答案 0 :(得分:0)
此外,Intellisense是高亮的布局,并说“布局”。属性未声明为警告。
在这种情况下,也是错误。 layout
应位于<target>
而不是<logger>
所以
<targets>
<target
name="logfile"
xsi:type="File"
fileName="log.txt"
archiveFileName="log.{#}.txt"
archiveNumbering="DateAndSequence"
archiveAboveSize="1000000"
archiveDateFormat="yyyyMMdd"
layout="${longdate}|${threadid}|${callsite-linenumber:skipFrames=Integer}|${level:uppercase=true}|${message}${newline}${exception:format=tostring}"
/>
...
和
<rules>
<logger name="StackTraceLogger" minlevel="Warn"
maxlevel="Error" writeTo="logfile" final="true" />
...
</rules>