Sus*_*hil 4 .net c# asp.net-mvc logging nlog
默认情况下,Nlog 日志记录到默认应用程序文件夹
<target xsi:type="File" name="f"
fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}
${exception:message=tostring}" />
Run Code Online (Sandbox Code Playgroud)
目前我的应用程序位于 C 目录中,我希望 Nlog 登录到特定文件夹中的 D 目录。我读到
fileName="${tempdir:folder=myapptmp}/sample.log"
Run Code Online (Sandbox Code Playgroud)
和
${specialfolder:dir=String:file=String:folder=Enum}
Run Code Online (Sandbox Code Playgroud)
似乎登录到“我的文档”、“图片”的特殊文件夹。所以没有多大用处。Tempdir 我不确定。有没有人之前做过这个或者对此有任何想法
我的 Nlog 配置
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<!-- add your targets here -->
<target name="File" xsi:type="File"
fileName="D:\Sushil\DwebLogging\log-${date:format=yyyy-MM-dd}.log"
layout="${longdate} ${uppercase:${level}} ${message} ${exception:
format=tostring}" />
<rules>
<logger name="*" minlevel="Trace" writeTo="File" />
</rules>
</nlog>
Run Code Online (Sandbox Code Playgroud)
只需在属性中指定完整路径,fileName如下所示;
<target name="File" xsi:type="File" fileName="D:\Logging\SomeFile-${date:format=yyyy-MM-dd}.log">
Run Code Online (Sandbox Code Playgroud)
完整的工作样本是;
<?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"
autoReload="true">
<targets>
<target name="File" xsi:type="File" fileName="D:\Logging\Sample-${date:format=yyyy-MM-dd}.csv">
<layout xsi:type="CsvLayout">
<column name="Index" layout="${counter}" />
<column name="ThreadID" layout="${threadid}" />
<column name="Time" layout="${longdate}" />
<column name="Severity" layout="${level:uppercase=true}" />
<column name="Location" layout="${callsite:className=False:fileName=True:includeSourcePath=False:methodName=False}" />
<column name="Detail" layout="${message}" />
<column name="Exception" layout="${exception:format=ToString}" />
</layout>
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="File" />
</rules>
</nlog>
Run Code Online (Sandbox Code Playgroud)
如果您仍然看不到文件,则应用程序运行所在的进程可能没有对该文件的写访问权限(默认情况下,NLog 将静默失败)。如果您的应用程序是 IIS 中的 Web 应用程序,则最常见这种情况,因为 IIS 进程需要对默认情况下没有的文件夹的写访问权限。