Search code examples
c#wpfazurelog4netazure-application-insights

How to set Telemetry Channel for Application Insights Log4Net Appender?


I added those Nuget Packages to my WPF App:

  • Microsoft.ApplicationInsights.Log4NetAppender
  • Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel

The logger is logging in a file, that works. But no data is transferred to Azure. I got this error:

  • AI: Server telemetry channel was not initialized. So persistent storage is turned off. You need to call ServerTelemetryChannel.Initialize(). Currently monitoring will continue but if telemetry cannot be sent it will be dropped.

My Question: Where (in the code) should i initialize the telemetry channel? And why do i have to do this? What's the appender for if i have to add a telemetry client (with config) anyways?


Solution

  • Update 0603:

    my app.config:

    enter image description here

    Debug with visual studio:

    enter image description here

    Update: please follow the screenshot below, and try to find the info you send. And if you still cannot find the info, please provide you detailed code(remove the personal/important data like instrumentation key, and also provide us the nuget package and version you're using).

    1.click the search button in overview page:

    enter image description here

    2.in the search screen, make the Local time and Event types are set correctly, then try to search the message:

    enter image description here


    You'd better provide the code to set log4net and app insights key.

    I did a simple test with wpf project, the below code works fine:

    public partial class MainWindow : Window
    {
    
        private static readonly ILog log = LogManager.GetLogger(typeof(MainWindow));
        public MainWindow()
        {
            TelemetryConfiguration.Active.InstrumentationKey = "the key";
            log4net.Config.XmlConfigurator.Configure();
    
            log.Info("wpf aaaa11111");
    
    
            InitializeComponent();
        }
     }
    

    You get the error "AI: Server telemetry channel was not initialized", maybe due to some incorrect configuration, like use the following code in the above working code:

    //when add the code, it will cause the error you mentioned.
    TelemetryConfiguration.Active.TelemetryChannel = new ServerTelemetryChannel();
    

    If you have to add a telemetry client (with config), and with proper configuration, both log4net and telemetry client can send data to application insights. The code like below:

    public partial class MainWindow : Window
    {
        private readonly TelemetryClient telemetryClient;
        private static readonly ILog log = LogManager.GetLogger(typeof(MainWindow));
        public MainWindow()
        {
            //configure the key here for log4net
            TelemetryConfiguration.Active.InstrumentationKey = "the key";
            log4net.Config.XmlConfigurator.Configure();
    
            var config = new TelemetryConfiguration();
    
            //configure the key here for telemetry client
            config.InstrumentationKey = "the key";
            telemetryClient = new TelemetryClient(config);
    
            log.Info("wpf aaaa333");
            log.Info(TelemetryConfiguration.Active.TelemetryChannel.ToString());
    
            telemetryClient.TrackTrace("it is going to start!");
    
            InitializeComponent();
        }
    }