简体   繁体   中英

NLog windows application log files not created after make an installable exe

I have created a windows application and log the error files in text files,when i published and make it as an application files, the log files is not created in the respective folder.

working fine- before convert into setup , the problem happens after installable exe. can anyone suggest to do a better solution app.config

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

added the nlog elements in app config

  <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>

Not sure if it is a bad copy-paste job, but you have invalid 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>

Should be changed to (include 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>

But good idea to enable and check the internal logger: https://github.com/NLog/NLog/wiki/Internal-Logging

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM