Naveed tejani
Naveed tejani

Reputation: 3

NLog not logging (Asp.net core with .net 4.6.2)

I am using asp.net core project with full .net framework 4.6.2 using NLog.Web.AspNetCore (4.3.1) nuget package. Following are my configurations.

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"
  autoReload="true"
  internalLogLevel="Warn"
  internalLogFile="c:\Logs\internal-nlog.txt">
  <!-- define various log targets -->
<targets>
  <!-- write logs to file -->
    <target xsi:type="File" name="allfile" fileName="c:\Logs\nlog-all-${shortdate}.log"
        layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception:format=stacktrace}"
        maxArchiveFiles="100"
        archiveFileName="c:\LogsBackup\nlog-all-${shortdate}.{##}.log"
        archiveNumbering="DateAndSequence"
        archiveAboveSize="10000000"
        archiveDateFormat="yyyyMMdd" />
    <target xsi:type="File" name="ownFile-web" fileName="c:\Logs\nlog-own-${shortdate}.log"
         layout="${longdate}|${logger}|${uppercase:${level}}|  ${message} ${exception:format=stacktrace}" />
    <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-web" />-->
</rules>
</nlog>

Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
  {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        //add nlog
        //env.ConfigureNLog("nlog.config");
        //loggerFactory.ConfigureNLog("nlog.config");
        //loggerFactory.AddNLog();
        app.AddNLogWeb();

        //add identity server authentication
        //app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
        //{
        //    Authority = "http://localhost:5000",
        //    RequireHttpsMetadata = false,
        //    ApiName = "prismapi"
        //});

        app.UseMvcWithDefaultRoute();

        app.UseSwagger();
        app.UseSwaggerUI(options =>
        {
            options.SwaggerEndpoint("/swagger/v1.0/swagger.json", "Prism api version 1");
        });
  }

For some reason, its not emitting logs. Nlog is not even creating its internal log file. Interestingly if I uncomment env.ConfigureNLog("nlog.config"); it starts to create at least its internal log file but still no success with the actual log file.

Upvotes: 0

Views: 2008

Answers (1)

hakany
hakany

Reputation: 7254

Probably you need to uncomment loggerFactory.AddNLog(); before app.AddNLogWeb(); in your Configure method.

env.ConfigureNLog("nlog.config"); should be in constructor according the examples in the documentation.

Please also look at How to use section in https://github.com/NLog/NLog.Extensions.Logging

I also want to advice to start with the same lines of code as in the examples. If that's working change them according your needs.

Hope this helps.

Upvotes: 1

Related Questions