7

Our current project consists of an ASP.NET application as well as a few smaller console applications. We are using Microsofts Application Insights for the application management of the ASP.NET app.
Now we want to integrate Application Insights in the console applications as well to be able to centralize our logging to Azure.
Our legacy logging is implemented with Log4Net. After configuring Application Insights Core and its corresponding Log4Net Appender in the console application we are able to see our log entries in Azure as expected. Every log message coming from the console application is sent to Azure, except for the logs which are getting an additional object appended from us. For example our log messages with severity level Error which contains the exception objects as second parameter:

Log.Error("This looks like an error", ex);

These log entries aren't displayed at all in Azure. They are only displayed when putting the exception object into the message like:

Log.Error($"This looks like an error. Exception: {ex}");

So it seems like there is a size limit regarding the exception objects from Log4Net but not the actual message? If so is this in any way configurable? Because changing every 'Log.Error();' in the whole project is not an option.

Our ApplicationInsights.config file:

<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
   <InstrumentationKey>[Our key]</InstrumentationKey>
   <TelemetryChannel Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.ServerTelemetryChannel, Microsoft.AI.ServerTelemetryChannel"/>

   <TelemetryProcessors>
      <Add Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.AdaptiveSamplingTelemetryProcessor, Microsoft.AI.ServerTelemetryChannel">
         <MaxTelemetryItemsPerSecond>5</MaxTelemetryItemsPerSecond>
         <ExcludedTypes>Trace;Exception</ExcludedTypes>
      </Add>
   </TelemetryProcessors>
</ApplicationInsights>

Edit:

Log4Net configuration:

<!-- ... -->
<!-- Configuration of logfile and console appender -->
<!-- ... -->

<root>
  <level value="ALL" />
  <appender-ref ref="logfile" />
  <appender-ref ref="console" />
  <appender-ref ref="aiAppender" />
</root>

<appender name="aiAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%message%newline" />
  </layout>
</appender>

There is no issue with the other appenders.

1
  • btw. Serilog does the same stuff - events containing Exceptions are sent as exception and not as a trace - it is documented here: github.com/serilog/serilog-sinks-applicationinsights I am not sure if this is good behavior, the message of the log event gets lost and only Exception is sent to App Insights Commented Sep 22, 2017 at 13:03

3 Answers 3

2

I figured it out. What caused the problem was that as soon as you append an exception object to any of the logging methods of Log4Net:

Log.Error(message, exception);
Log.Warn(message, exception);
Log.Info(message, exception);

The specifc log entry isn't handled as trace but as exception in Application Insights. So adjusting the filter in the Application Insights tab in Azure to also show exceptions was enough. Now I'm able to see the Log.Error(message, exception) entries in Azure as well.

I don't know why this is handled like this, because this makes processing the logs more complex for third party software (For example: Exceptions doesn't have an explicit message field in Application Insights, etc.).

Sign up to request clarification or add additional context in comments.

3 Comments

did you find a solution for this to send the logs as trace instead as exception? I have the same problem as you. The exceptions are logged in Application Insights but the message part gets lost.
Unfortunately not.
I solved this problem by sending 2 events to App Insights - Message with Exception will be sent separated. Message as Trace and Exception as Exception. In App Insights you can join Traces with Exceptions by Operation_ID and get wanted result.
2

As long as you are passing an exception it will be logged as an exception whatever the type of logging method you are using .. if you want it to be logged as trace you shouldn't pass an exception. Which makes sense

Comments

0

You need to configure log4net, you are now only configuring Application Insights.

the steps to take:

  • configure log4net
  • add an application insights appender to let log4net log to application insights

Next I would look at the version you use. I see you are using .net core, and it could be the package was build against an other version of the application insights dll. Try to get the latest version of the Microsoft.ApplicationInsights.Log4NetAppender.

Microsoft.ApplicationInsights.Log4NetAppender latest version

1 Comment

Installed is the latest stable version (which is currently 2.2.0) of the Log4NetAppender. As well as the latest stable version (2.4.0) for ApplicationInsights Core.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.