在创建可安装的exe之后,不会创建NLog Windows应用程序日志文件

时间:2018-03-19 11:51:32

标签: c# nlog windows-applications

我创建了一个Windows应用程序并将错误文件记录在文本文件中,当我发布并将其作为应用程序文件制作时,日志文件不会在相应的文件夹中创建。

正常工作 - 在转换为设置之前,问题发生在可安装的exe之后。 任何人都可以建议做更好的解决方案 的app.config

 <target name="logfile" xsi:type="File" fileName="${basedir}/DebugLog/${date:format=yyyy-MM-dd}-logFile.log" />

在app config中添加了nlog元素

  <nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
      <target name="logfile" xsi:type="File" fileName="${basedir}/DebugLog/${date:format=yyyy-MM-dd}-logFile.log" />

   <rules>
      <logger name="*" minlevel="Trace" writeTo="logfile" />
      <logger name="*" minlevel="Error" writeTo="email" />
    </rules>
  </nlog>

C#:

  private void button3_Click(object sender, EventArgs e)
        {
                logger.Error("Some Error has occured ");
        }

的app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
      <target name="logfile" xsi:type="File" fileName="${basedir}/DebugLog/${date:format=yyyy-MM-dd}-logFile.log" />



    <rules>
      <logger name="*" minlevel="Trace" writeTo="logfile" />
      <logger name="*" minlevel="Error" writeTo="email" />
    </rules>
  </nlog>

  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE,OPTIONS" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>


  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

1 个答案:

答案 0 :(得分:2)

不确定它是否是错误的复制粘贴作业,但您的xml无效:

  <nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
      <target name="logfile" xsi:type="File" fileName="${basedir}/DebugLog/${date:format=yyyy-MM-dd}-logFile.log" />

    <rules>
      <logger name="*" minlevel="Trace" writeTo="logfile" />
      <logger name="*" minlevel="Error" writeTo="email" />
    </rules>
  </nlog>

应更改为(包括end-targets-tag):

  <nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
      <target name="logfile" xsi:type="File" fileName="${basedir}/DebugLog/${date:format=yyyy-MM-dd}-logFile.log" />
    </targets>
    <rules>
      <logger name="*" minlevel="Trace" writeTo="logfile" />
      <logger name="*" minlevel="Error" writeTo="email" />
    </rules>
  </nlog>

但最好启用并检查内部记录器:https://github.com/NLog/NLog/wiki/Internal-Logging