microsoft / ApplicationInsights-dotnet-logging

.NET Logging adaptors
106 stars 49 forks source link

Does Microsoft. ApplicationInsights. NLogTarget have influence on NLog console and file targets? #266

Closed imeya closed 5 years ago

imeya commented 5 years ago

I know this issue has been asked before, but the publisher closed it for no reason. So I ask it again.

when use Microsoft.ApplicationInsights.NLogTarget as a target write log to Azure Application Insights, but I found my console and file targets didn't work, Microsoft.ApplicationInsights.NLogTarget is ok, what's the reason?

note: write to console and write to file are configured in the nlog.config.

snakefoot commented 5 years ago

Have you tried to update <extensions> in NLog.config?

<extensions>
      <add assembly="Microsoft.ApplicationInsights.NLogTarget" />
</extensions>

See also: https://cmatskas.com/working-with-application-insights-and-nlog-in-console-apps-net/

snakefoot commented 5 years ago

Maybe add some details of how you configure NLog at runtime, and how NLog.config looks like.

Saying that you have some random hidden code that doesn't work is hard to help you with.

imeya commented 5 years ago

@snakefoot , thanks for your reply. I add my code and config file.

program.cs(.net framework console project):

class Program
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        static void Main(string[] args)
        {  
            var client = new TelemetryClient();
            TelemetryConfiguration.Active.InstrumentationKey = "xxxx";

            client.TrackEvent("333 app insights now sending a custom event!!!");
            logger.Trace("Nlog :this is a trace message!!!");
            logger.Debug("Nlog: this is a debug message!!!");
            logger.Info("Nlog: this is a info debug!!!");

            Console.ReadLine();
        }
    }

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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">

  <!-- optional, add some variables
  https://github.com/nlog/NLog/wiki/Configuration-file#variables
  -->
  <variable name="myvar" value="myvalue"/>
  <!--
  See https://github.com/nlog/nlog/wiki/Configuration-file
  for information on customizing logging rules and outputs.
   -->
  <targets>    
    <target xsi:type="File" name="file" fileName="${basedir}/logs/${shortdate}.log"
        layout="${longdate} ${uppercase:${level}} ${message}" />

    <target xsi:type="Console" name="console"
          layout="${longdate}|${uppercase:${level}}|${message}" />   
  </targets>

  <rules>
    <logger name="*" minlevel="Debug" writeTo="file" />
    <logger name="*" minlevel="Debug" writeTo="console" />

  </rules>
  <!--I also tried add this below, but not work, only can see the logs in application insights in azure portal-->
<extensions>
  <add assembly="Microsoft.ApplicationInsights.NLogTarget" />
</extensions>
</nlog>

my app.config(this is by default after install the package Microsoft.ApplicationInsights.NLogTarget):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
    </startup>
    <nlog>
        <extensions>
            <add assembly="Microsoft.ApplicationInsights.NLogTarget"/>
        </extensions>
        <targets>
            <target type="ApplicationInsightsTarget" name="aiTarget"/>
        </targets>      
        <rules>
            <logger name="*" minlevel="Trace" writeTo="aiTarget"/>
            <!--I also tried add the following, but not works for console and file-->
          <logger name="*" minlevel="Debug" writeTo="file" />
          <logger name="*" minlevel="Debug" writeTo="console" />
        </rules>
    </nlog>
</configuration>
snakefoot commented 5 years ago

Usually you only have one NLog-config. Unless you are making use of include-statement.

Have you tried to merge the NLog-config in app.config-file with NLog.config-file into a single file?

imeya commented 5 years ago

@snakefoot , I will take a try, and let you know if it works. thanks again.

imeya commented 5 years ago

@snakefoot , thanks very much for the merge idea, it works:)

fionik commented 5 years ago

I have encountered the same problem. What I discovered is that simply adding Microsoft.ApplicationInsights.NLogTarget package into the project without any changes to the code or NLog.conf instantly "disables" logging to console and file targets.

imeya commented 5 years ago

@fionik , thanks for your idea. I'll take a try. And another solution is that merge the 2 config files.

mrtristan commented 5 years ago

@fionik i've encountered this as well where if the app insights target is enabled, file logging stops working.

@Dmitry-Matveev is this something that is an expected behavior at all?