Srv*_*v19 31 c# compact-framework nlog
我正在尝试将日志记录添加到使用Windows Mobile 6.1在移动设备上运行的应用程序. .NETCompact框架3.5.使用NLog.
我安装了适当版本的NLog发行版.
但是,没有创建日志文件.
这是我的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">
<targets>
<target name="logfile" xsi:type="File" fileName=".\Neolant.ASRM.Terminal.log" layout="${longdate}|${level}|${message}|${exception}" autoFlush="true"/>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logfile" />
</rules>
</nlog>
Run Code Online (Sandbox Code Playgroud)
这是我使用的测试代码:
public static void Main()
{
try
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
var logger = NLog.LogManager.GetLogger("UpperLevel");
logger.Info("test test test.");
try
{
throw new Exception("Unexpected!");
}
catch (Exception e)
{
var logger = NLog.LogManager.GetLogger("UpperLevel");
logger.WarnException("An exception occured.", e);
}
throw new Exception("Suddenly!");
}
finally
{
NLog.LogManager.Flush();
}
}
private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
{
var logger = NLog.LogManager.GetLogger("UpperLevel");
logger.FatalException("Application closed due to exception.", unhandledExceptionEventArgs.ExceptionObject as Exception);
NLog.LogManager.Flush();
}
Run Code Online (Sandbox Code Playgroud)
Mal*_*son 25
我遇到了这个问题,我的日志文件没有被复制到我的构建目录.NLog github页面有答案.(为了更好的可读性,我重新格式化了一段.) https://github.com/NLog/NLog/wiki/Logging-troubleshooting
NLog无法找到配置文件.当NLog.config文件配置为Build Action = None或Copy to Output Directory =不在Visual Studio中复制时,可能会发生这种情况.
设置构建操作=内容和"复制到输出目录=如果更新则复制以修复此问题"
Srv*_*v19 21
正在创建日志文件 - 但不在应用程序目录中.
使用$ {basedir}布局渲染器作为文件名的一部分被证明是一种解决方案.
Mau*_*rez 15
如果标记为答案的响应不是那么清楚,您可以查看示例
<targets>
<target xsi:type="Console" name="console"
layout="${longdate}|${level}|${message}" />
<target xsi:type="File" name="ErrorLog" fileName="${basedir}/error.txt"
layout="${longdate}
Trace: ${stacktrace}
${message}" />
<target xsi:type="File" name="AccessLog" fileName="${basedir}/access.txt"
layout="${shortdate} | ${message}" />
</targets>
Run Code Online (Sandbox Code Playgroud)
我的问题与权限相关,日志文件需要允许进程写入它,如果没有写入权限,您将无法获得文件。
以下是修复 IIS 中网站的方法:
安全脚注:从安全角度来看,最佳实践是使用 ApplicationPoolIdentity,因为它是动态创建的非特权帐户。更多阅读此处:https ://learn.microsoft.com/en-us/iis/manage/configuring-security/application-pool-identities
从 nlog 故障排除指南:
https://github.com/NLog/NLog/wiki/Logging-troubleshooting
如果您知道您的配置文件肯定会被找到,请暂时将 NLog.config 文件的部分替换为以下内容并尝试。
此规则将匹配您创建的任何记录器,如果它有效,它将在“基本目录”中放置一个 log.txt 文件 - 这是您的测试实例的“bin”目录,例如,如果您正在调试中运行模式,您将在 bin > debug 文件夹中看到 log.txt。(这在故障排除指南中没有很清楚地解释)。
如果这有效,那么您就知道问题出在您的规则上:
<nlog throwExceptions="true">
<targets>
<target name="file" type="File" fileName="${basedir}/log.txt" />
</targets>
<rules>
<logger name="*" minLevel="Trace" writeTo="file" />
</rules>
</nlog>
Run Code Online (Sandbox Code Playgroud)
我发现只有 name="file" 对目标有效 - 其他值没有
如上所述添加 throwExceptions="true" 还可以确保您在调试时获得有用的错误消息。
从nlog故障排除指南
请检查Nlog.config文件属性:复制到输出目录应始终复制
请查看图片链接https://i.stack.imgur.com/AlUG5.png
| 归档时间: |
|
| 查看次数: |
53091 次 |
| 最近记录: |