我正在尝试在控制台应用程序中记录异常。我一如既往地做了一切(然后它对我有用......):
NLog.config:
<?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">
<targets>
<target name="LogFile" xsi:type="File"
fileName="${basedir}/Log/${date:format=yyyyMMdd} myLog.txt"
layout="${longdate}|${level:uppercase=true}|${message}|${exception}" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="LogFile" />
</rules>
</nlog>
class Program
{
private static Logger _log = LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
_log.Error("test");
}
}
出于某种神秘的原因,我的计算机上永远不会创建文件。任何想法为什么
答案 0 :(得分:2)
您的配置看起来有效,因此不应该成为问题。
要检查的一些事项:
您是否在正确的目录中使用了nlog.config?测试的最佳方法是将nlog.config复制到包含.dll或.exe的目录。
启用例外以确保Nlog未捕获错误:throwExceptions="true"
通过设置internalLogLevel="Info"
检查内部日志,然后选中&#34; c:\ temp \ nlog-internal.log&#34;
可以在NLog documentation上找到完整的故障排除教程。
答案 1 :(得分:1)
修改您的NLog
配置,更改您的日志文件路径,使用${shortdate}
代替{date:format=yyyyMMdd}
。在规则中设置名称并像在此示例中一样调用它。
<?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">
<targets async="true">
<targets>
<target name="LogFile" xsi:type="File"
fileName="${basedir}/Log/${shortdate}myLog.txt"
layout="${longdate}|${level:uppercase=true}|${message}|${exception}" />
</targets>
<rules>
<logger name="ErrorLog" minlevel="Trace" writeTo="LogFile" />
</rules>
</nlog>
和你的.cs
class Program
{
private Logger ErrorLogger = NLog.LogManager.GetLogger("ErrorLog");
static void Main(string[] args)
{
ErrorLogger.Error("test");
}
}
答案 2 :(得分:0)