1

I am using NLog in my Asp.net Core Application, but NLog is not able to write in file, i have all ready change permission of file,but still facing proble. file gets created,on first time but unable to write log in that file.

Here is my nlog.config file code below:

<?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"
      internalLogLevel="Warn"
      internalLogFile="C:\git\damienbod\AspNetCoreNlog\Logs\internal-nlog.txt">

    <targets>
      <target xsi:type="File" name="allfile" fileName="D:\nlog-all.txt"
                  layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}"
                  keepFileOpen="false"/>
      <target xsi:type="EventLog"
          name="String"
          layout="Layout"
          machineName="String"
          source="Layout"
          category="Layout"
          eventId="Layout"
          log="String"
          maxMessageLength="Integer" />
    </targets>

    <rules>
      <!--All logs, including from Microsoft-->
      <logger name="*" minlevel="*Info*" maxLevel="*Deubg*" writeTo="allfile" />
    </rules>
  </nlog>

And here is code of my controller

NLog.Logger _Logger= LogManager.GetCurrentClassLogger();
            _Logger.Info("Hello i m executing");
Julian
  • 33,915
  • 22
  • 119
  • 174
Jani Devang
  • 1,099
  • 12
  • 20
  • I have make changes as per your suggestion @The FORCE JB, minlevel="Debug" but still facing same problem – Jani Devang Nov 14 '16 at 09:30
  • Is there any error message in internalLogFile? Seems no other incorrect settings, I have a workable NLog.config in [github](https://github.com/KarateJB/Angular2.Mvc/blob/master/Angular2.Mvc/src/Angular2.Mvc.Website/NLog.config), hope it helps! – KarateJB Nov 17 '16 at 15:26

4 Answers4

1

Just follow this link: https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-(csproj---vs2017)

You have to enable copy to bin folder for nlog.config Image copy nlog.config

Community
  • 1
  • 1
  • links only answer are prone to be moderated please add some explanation from the provided link. – Abhishek Gurjar Jul 03 '17 at 16:37
  • Nlog.config needs to be copied at compilation to the bin folder. So go on nlog.config properties and select the option "Copy Always" for section named "Copy to output directory" :) Hope this helps and clearer than the previous answer – Pierre Cangem's Jul 03 '17 at 16:41
0

I think it's because the "Info" level is actually has the higher priority than "Debug". Change the config like this might works.

 <logger name="*" minlevel="Debug" maxLevel="Info" writeTo="File" />

Reference : NLOG Configuration

KarateJB
  • 941
  • 2
  • 10
  • 22
0

I think your config file should like this.

<?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"
      internalLogLevel="Warn"
      internalLogFile="C:\git\damienbod\AspNetCoreNlog\Logs\internal-nlog.txt">

  <!-- define various log targets -->
  <targets>
    <!-- write logs to file -->
     <target xsi:type="File" name="allfile" fileName="D:\nlog-all.txt"
                 layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" keepFileOpen="false"/>

  </targets>

  <rules>
    <logger name="*" minlevel="Debug" maxlevel="Info"  writeTo="allfile" />
  </rules>
</nlog>

I have removed unnecessary part and also mixlevel and maxlevel as suggested by @The FORCE Jb you have to specify.

Apart from this I can also see that you have type for DEBUG word.

Julian
  • 33,915
  • 22
  • 119
  • 174
dotnetstep
  • 17,065
  • 5
  • 54
  • 72
0

You need the NLog.Extensions.Logging package (NuGet link) for .NET Core and enable NLog in startup.cs:

public void Configure(IApplicationBuilder app, 
                      IHostingEnvironment env,  ILoggerFactory loggerFactory)
{
  //add NLog to ASP.NET Core
  loggerFactory.AddNLog();

  //configure nlog.config in your project root
  env.ConfigureNLog("nlog.config");

  ...

For more information, see the GitHub page

Julian
  • 33,915
  • 22
  • 119
  • 174