0
votes

I've just created an empty ASP.NET 5 web appliction and I want to use NLog for my logs. So far I've successfully installed NLog and created a public static Logger in the Startup class. In my application folder I've created the NLog.config file with the following content:

<?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="file.txt" />
    </targets>
    <rules>
        <logger name="*" minlevel="Info" writeTo="logfile" />
    </rules>
</nlog>

In the Configure method of Startup class I'm trying to log some data with the following lines of code:

logger.Warn("foo1");
logger.Debug("foo2");

The project builds successfully but after running it I don't see any file.txt whatsoever.

3

3 Answers

1
votes

I have found a possible solution. After creating an ASP.NET 5 empty project you can do the following:

1) Open the Startup.cs file and add the following usings:

using Microsoft.Extensions.Logging;
using NLog.Framework.Logging;

Notice that in order to add the second using you have to modify the package.json file by adding the following dependency:

"NLog.Framework.logging": "1.0.0-rc1-final"

2) In the startup.cs file you must modify the signature of the "Configure" method by adding the arguments ihe and ilf. So it will eventually look like this.

Configure(IApplicationBuilder app, IHostingEnvironment ihe, ILoggerFactory ilf)

The arguments ihe and ifg are to be used in this way:

ilf.AddNLog();
ihe.ConfigureNLog("nlog.config");
ILogger logger = ilf.CreateLogger(GetType().Namespace);
logger.LogInformation("i am nlog bye bye");

3) In the project folder, that is not wwwroot but its parent folder, add a file and call it "nlog.config". It can be filled with the following contnent:

<?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:\temp\internal.txt">


  <!-- define various log targets -->
  <targets>
    <!-- write logs to file -->
    <target xsi:type="File" name="allfile" fileName="c:\temp\nlog-all-${shortdate}.log"
                 layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />

    <target xsi:type="File" name="ownFile" fileName="c:\temp\nlog-own-${shortdate}.log"
              layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />

    <target xsi:type="Null" name="blackhole" />
  </targets>

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

    <!--Skip Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
    <logger name="*" minlevel="Trace" writeTo="ownFile" />
  </rules>
</nlog>

Now if you run the project and go to C:\temp you will see two files: "nlog-all-.log" and "nlog-own-.log". In the second file you'll see the message "i am nlog bye bye"

0
votes

Provide full path for log file

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

  <!-- 
  See http://nlog-project.org/wiki/Configuration_file 
  for information on customizing logging rules and outputs.
   -->
  <targets>
    <!-- add your targets here -->
    <target name="asyncFile" xsi:type="AsyncWrapper">
      <target name="logfile" xsi:type="File" fileName="C:\Log.txt" layout="${longdate} ${callsite} ${level} ${message} ${newline}" />
    </target>
    <!--
    <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />
    -->
  </targets>

  <rules>
    <!-- add your logging rules here -->
    <logger name="*" minlevel="Info" writeTo="asyncFile" />
    <!--
    <logger name="*" minlevel="Trace" writeTo="f" />
    -->
  </rules>
</nlog>
0
votes

This is the NLogConfiguration which i had used and it's absoulty working Fine

 <?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">
        <targets >
    <target xsi:type="File" name="logfile"
            fileName="${basedir}/logs/YourFileName.log"
            layout="${longdate} ${level:uppercase=true:padding=5} ${gdc:item=hostname} ${gdc:item=useremail} (${logger} - ${mdc:item=actionname})  ${message} ${exception:format=tostring}"
            archiveEvery="Day"
            archiveFileName ="${basedir}/logs/YourFileName.${date:format=yyyy-MM-dd HH.mm}.{#}.log"
            archiveNumbering ="Sequence"
            maxArchiveFiles="30"
            fileAttributes="Compressed">
    </target>
       </targets>
       <rules>
       <logger name="*" minlevel="Debug" writeTo="logfile">
      </logger>
      </rules>
    </nlog>

and on server side

public class YourClassName
{
   private static Logger log = LogManager.GetCurrentClassLogger();

  public void YourMethodName()
    {
      try
        {
          log.Info("Success");
        }
        catch(Exception Ex)
        {
         log.Info("Exception"+ex);
        }
       } 
   }