NLog for asp.net core is not filtering Microsoft logs












0















I have the following nlog.config file in ASP.NET Core 2.1 project. However, it's logging messages from every logger (including Microsoft logger) to console.



<?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="Error"
internalLogFile="${specialfolder:folder=UserProfile}nloginternal-nlog.txt">

<variable name="fullLayout" value="${shortdate} [${uppercase:${level}}] ${logger}: ${message} ${exception:format=tostring} url: ${aspnet-request-url}" />

<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>

<!-- the targets to write to -->
<targets>
<!-- write to console -->
<target name="console" xsi:type="ColoredConsole" layout="${fullLayout}" />

<!-- write to file -->
<target xsi:type="File"
name="allfile"
fileName="${defaultDirectory}/test-api-all.log"
archiveEvery="Day"
archiveFileName="${defaultDirectory}/test-api-all-${#}.log"
archiveNumbering="Date"
archiveDateFormat="yyyy-MM-dd"
maxArchiveFiles="5"
layout="${fullLayout}" />

<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File"
name="ownFile-web"
fileName="${defaultDirectory}/test-api-app.log"
archiveEvery="Day"
archiveFileName="${defaultDirectory}/test-api-app-${#}.log"
archiveNumbering="Date"
archiveDateFormat="yyyy-MM-dd"
maxArchiveFiles="5"
layout="${fullLayout}" />
</targets>

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

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


I do not have any logging settings in appsettings.json files. All logger configuration in in nlog.config. In the Program.cs, I'm registering NLog like:



public static IWebHost BuildWebHost(string args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging(logging => logging.ClearProviders())
.UseNLog()
.Build();


How can I filter out Microsoft logs?



EDIT:



Above configuration started working without a problem the next day without me making any changes :O










share|improve this question





























    0















    I have the following nlog.config file in ASP.NET Core 2.1 project. However, it's logging messages from every logger (including Microsoft logger) to console.



    <?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="Error"
    internalLogFile="${specialfolder:folder=UserProfile}nloginternal-nlog.txt">

    <variable name="fullLayout" value="${shortdate} [${uppercase:${level}}] ${logger}: ${message} ${exception:format=tostring} url: ${aspnet-request-url}" />

    <!-- enable asp.net core layout renderers -->
    <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
    </extensions>

    <!-- the targets to write to -->
    <targets>
    <!-- write to console -->
    <target name="console" xsi:type="ColoredConsole" layout="${fullLayout}" />

    <!-- write to file -->
    <target xsi:type="File"
    name="allfile"
    fileName="${defaultDirectory}/test-api-all.log"
    archiveEvery="Day"
    archiveFileName="${defaultDirectory}/test-api-all-${#}.log"
    archiveNumbering="Date"
    archiveDateFormat="yyyy-MM-dd"
    maxArchiveFiles="5"
    layout="${fullLayout}" />

    <!-- another file log, only own logs. Uses some ASP.NET core renderers -->
    <target xsi:type="File"
    name="ownFile-web"
    fileName="${defaultDirectory}/test-api-app.log"
    archiveEvery="Day"
    archiveFileName="${defaultDirectory}/test-api-app-${#}.log"
    archiveNumbering="Date"
    archiveDateFormat="yyyy-MM-dd"
    maxArchiveFiles="5"
    layout="${fullLayout}" />
    </targets>

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

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


    I do not have any logging settings in appsettings.json files. All logger configuration in in nlog.config. In the Program.cs, I'm registering NLog like:



    public static IWebHost BuildWebHost(string args) =>
    WebHost.CreateDefaultBuilder(args)
    .UseStartup<Startup>()
    .ConfigureLogging(logging => logging.ClearProviders())
    .UseNLog()
    .Build();


    How can I filter out Microsoft logs?



    EDIT:



    Above configuration started working without a problem the next day without me making any changes :O










    share|improve this question



























      0












      0








      0








      I have the following nlog.config file in ASP.NET Core 2.1 project. However, it's logging messages from every logger (including Microsoft logger) to console.



      <?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="Error"
      internalLogFile="${specialfolder:folder=UserProfile}nloginternal-nlog.txt">

      <variable name="fullLayout" value="${shortdate} [${uppercase:${level}}] ${logger}: ${message} ${exception:format=tostring} url: ${aspnet-request-url}" />

      <!-- enable asp.net core layout renderers -->
      <extensions>
      <add assembly="NLog.Web.AspNetCore"/>
      </extensions>

      <!-- the targets to write to -->
      <targets>
      <!-- write to console -->
      <target name="console" xsi:type="ColoredConsole" layout="${fullLayout}" />

      <!-- write to file -->
      <target xsi:type="File"
      name="allfile"
      fileName="${defaultDirectory}/test-api-all.log"
      archiveEvery="Day"
      archiveFileName="${defaultDirectory}/test-api-all-${#}.log"
      archiveNumbering="Date"
      archiveDateFormat="yyyy-MM-dd"
      maxArchiveFiles="5"
      layout="${fullLayout}" />

      <!-- another file log, only own logs. Uses some ASP.NET core renderers -->
      <target xsi:type="File"
      name="ownFile-web"
      fileName="${defaultDirectory}/test-api-app.log"
      archiveEvery="Day"
      archiveFileName="${defaultDirectory}/test-api-app-${#}.log"
      archiveNumbering="Date"
      archiveDateFormat="yyyy-MM-dd"
      maxArchiveFiles="5"
      layout="${fullLayout}" />
      </targets>

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

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


      I do not have any logging settings in appsettings.json files. All logger configuration in in nlog.config. In the Program.cs, I'm registering NLog like:



      public static IWebHost BuildWebHost(string args) =>
      WebHost.CreateDefaultBuilder(args)
      .UseStartup<Startup>()
      .ConfigureLogging(logging => logging.ClearProviders())
      .UseNLog()
      .Build();


      How can I filter out Microsoft logs?



      EDIT:



      Above configuration started working without a problem the next day without me making any changes :O










      share|improve this question
















      I have the following nlog.config file in ASP.NET Core 2.1 project. However, it's logging messages from every logger (including Microsoft logger) to console.



      <?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="Error"
      internalLogFile="${specialfolder:folder=UserProfile}nloginternal-nlog.txt">

      <variable name="fullLayout" value="${shortdate} [${uppercase:${level}}] ${logger}: ${message} ${exception:format=tostring} url: ${aspnet-request-url}" />

      <!-- enable asp.net core layout renderers -->
      <extensions>
      <add assembly="NLog.Web.AspNetCore"/>
      </extensions>

      <!-- the targets to write to -->
      <targets>
      <!-- write to console -->
      <target name="console" xsi:type="ColoredConsole" layout="${fullLayout}" />

      <!-- write to file -->
      <target xsi:type="File"
      name="allfile"
      fileName="${defaultDirectory}/test-api-all.log"
      archiveEvery="Day"
      archiveFileName="${defaultDirectory}/test-api-all-${#}.log"
      archiveNumbering="Date"
      archiveDateFormat="yyyy-MM-dd"
      maxArchiveFiles="5"
      layout="${fullLayout}" />

      <!-- another file log, only own logs. Uses some ASP.NET core renderers -->
      <target xsi:type="File"
      name="ownFile-web"
      fileName="${defaultDirectory}/test-api-app.log"
      archiveEvery="Day"
      archiveFileName="${defaultDirectory}/test-api-app-${#}.log"
      archiveNumbering="Date"
      archiveDateFormat="yyyy-MM-dd"
      maxArchiveFiles="5"
      layout="${fullLayout}" />
      </targets>

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

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


      I do not have any logging settings in appsettings.json files. All logger configuration in in nlog.config. In the Program.cs, I'm registering NLog like:



      public static IWebHost BuildWebHost(string args) =>
      WebHost.CreateDefaultBuilder(args)
      .UseStartup<Startup>()
      .ConfigureLogging(logging => logging.ClearProviders())
      .UseNLog()
      .Build();


      How can I filter out Microsoft logs?



      EDIT:



      Above configuration started working without a problem the next day without me making any changes :O







      c# nlog asp.net-core-2.1






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 14:16







      swdon

















      asked Jan 1 at 14:38









      swdonswdon

      1,01411231




      1,01411231
























          2 Answers
          2






          active

          oldest

          votes


















          3














          Maybe this will work:



            <!-- rules to map from logger name to target -->
          <rules>
          <!--Skip non-critical Microsoft logs and so log only own logs-->
          <logger name="Microsoft.*" maxLevel="Info" final="true" />

          <logger name="*" minlevel="Trace" writeTo="console" />

          <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
          </rules>


          The order of the logging rules are important, as the rules are matched from the top. See also https://github.com/NLog/NLog/wiki/Configuration-file#rules






          share|improve this answer
























          • Somehow, my original config started working without a problem the next day... Upvoted your answer, thanks for taking time to help me :)

            – swdon
            Jan 2 at 14:15



















          1














          The order of your logging rules seems to be correct. I suggest trying to double check if your updated nlog.config file is being copied to the build directory (e.g. binDebugnetcoreapp2.1nlog.config)



          I kind of encountered the same issue, but found out that debugging using Visual Studio, it sometimes doesn't really copy the nlog.config file to your build directory. So the solution is to Clean the project first, then Build, and finally Debug.






          share|improve this answer
























          • Yes, I saw this happened too. But on this particular occasion, I had tried cleaning (even manually deleting the files in the bin folder). My guess is some caching was messing with it.

            – swdon
            Jan 9 at 16:53











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996340%2fnlog-for-asp-net-core-is-not-filtering-microsoft-logs%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          Maybe this will work:



            <!-- rules to map from logger name to target -->
          <rules>
          <!--Skip non-critical Microsoft logs and so log only own logs-->
          <logger name="Microsoft.*" maxLevel="Info" final="true" />

          <logger name="*" minlevel="Trace" writeTo="console" />

          <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
          </rules>


          The order of the logging rules are important, as the rules are matched from the top. See also https://github.com/NLog/NLog/wiki/Configuration-file#rules






          share|improve this answer
























          • Somehow, my original config started working without a problem the next day... Upvoted your answer, thanks for taking time to help me :)

            – swdon
            Jan 2 at 14:15
















          3














          Maybe this will work:



            <!-- rules to map from logger name to target -->
          <rules>
          <!--Skip non-critical Microsoft logs and so log only own logs-->
          <logger name="Microsoft.*" maxLevel="Info" final="true" />

          <logger name="*" minlevel="Trace" writeTo="console" />

          <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
          </rules>


          The order of the logging rules are important, as the rules are matched from the top. See also https://github.com/NLog/NLog/wiki/Configuration-file#rules






          share|improve this answer
























          • Somehow, my original config started working without a problem the next day... Upvoted your answer, thanks for taking time to help me :)

            – swdon
            Jan 2 at 14:15














          3












          3








          3







          Maybe this will work:



            <!-- rules to map from logger name to target -->
          <rules>
          <!--Skip non-critical Microsoft logs and so log only own logs-->
          <logger name="Microsoft.*" maxLevel="Info" final="true" />

          <logger name="*" minlevel="Trace" writeTo="console" />

          <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
          </rules>


          The order of the logging rules are important, as the rules are matched from the top. See also https://github.com/NLog/NLog/wiki/Configuration-file#rules






          share|improve this answer













          Maybe this will work:



            <!-- rules to map from logger name to target -->
          <rules>
          <!--Skip non-critical Microsoft logs and so log only own logs-->
          <logger name="Microsoft.*" maxLevel="Info" final="true" />

          <logger name="*" minlevel="Trace" writeTo="console" />

          <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
          </rules>


          The order of the logging rules are important, as the rules are matched from the top. See also https://github.com/NLog/NLog/wiki/Configuration-file#rules







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 17:39









          Rolf KristensenRolf Kristensen

          5,7312435




          5,7312435













          • Somehow, my original config started working without a problem the next day... Upvoted your answer, thanks for taking time to help me :)

            – swdon
            Jan 2 at 14:15



















          • Somehow, my original config started working without a problem the next day... Upvoted your answer, thanks for taking time to help me :)

            – swdon
            Jan 2 at 14:15

















          Somehow, my original config started working without a problem the next day... Upvoted your answer, thanks for taking time to help me :)

          – swdon
          Jan 2 at 14:15





          Somehow, my original config started working without a problem the next day... Upvoted your answer, thanks for taking time to help me :)

          – swdon
          Jan 2 at 14:15













          1














          The order of your logging rules seems to be correct. I suggest trying to double check if your updated nlog.config file is being copied to the build directory (e.g. binDebugnetcoreapp2.1nlog.config)



          I kind of encountered the same issue, but found out that debugging using Visual Studio, it sometimes doesn't really copy the nlog.config file to your build directory. So the solution is to Clean the project first, then Build, and finally Debug.






          share|improve this answer
























          • Yes, I saw this happened too. But on this particular occasion, I had tried cleaning (even manually deleting the files in the bin folder). My guess is some caching was messing with it.

            – swdon
            Jan 9 at 16:53
















          1














          The order of your logging rules seems to be correct. I suggest trying to double check if your updated nlog.config file is being copied to the build directory (e.g. binDebugnetcoreapp2.1nlog.config)



          I kind of encountered the same issue, but found out that debugging using Visual Studio, it sometimes doesn't really copy the nlog.config file to your build directory. So the solution is to Clean the project first, then Build, and finally Debug.






          share|improve this answer
























          • Yes, I saw this happened too. But on this particular occasion, I had tried cleaning (even manually deleting the files in the bin folder). My guess is some caching was messing with it.

            – swdon
            Jan 9 at 16:53














          1












          1








          1







          The order of your logging rules seems to be correct. I suggest trying to double check if your updated nlog.config file is being copied to the build directory (e.g. binDebugnetcoreapp2.1nlog.config)



          I kind of encountered the same issue, but found out that debugging using Visual Studio, it sometimes doesn't really copy the nlog.config file to your build directory. So the solution is to Clean the project first, then Build, and finally Debug.






          share|improve this answer













          The order of your logging rules seems to be correct. I suggest trying to double check if your updated nlog.config file is being copied to the build directory (e.g. binDebugnetcoreapp2.1nlog.config)



          I kind of encountered the same issue, but found out that debugging using Visual Studio, it sometimes doesn't really copy the nlog.config file to your build directory. So the solution is to Clean the project first, then Build, and finally Debug.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 9 at 15:31









          JuliennJulienn

          54111




          54111













          • Yes, I saw this happened too. But on this particular occasion, I had tried cleaning (even manually deleting the files in the bin folder). My guess is some caching was messing with it.

            – swdon
            Jan 9 at 16:53



















          • Yes, I saw this happened too. But on this particular occasion, I had tried cleaning (even manually deleting the files in the bin folder). My guess is some caching was messing with it.

            – swdon
            Jan 9 at 16:53

















          Yes, I saw this happened too. But on this particular occasion, I had tried cleaning (even manually deleting the files in the bin folder). My guess is some caching was messing with it.

          – swdon
          Jan 9 at 16:53





          Yes, I saw this happened too. But on this particular occasion, I had tried cleaning (even manually deleting the files in the bin folder). My guess is some caching was messing with it.

          – swdon
          Jan 9 at 16:53


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996340%2fnlog-for-asp-net-core-is-not-filtering-microsoft-logs%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          How to fix TextFormField cause rebuild widget in Flutter