Konnect.Log4NetAlaAppender 1.1.7026.17263

dotnet add package Konnect.Log4NetAlaAppender --version 1.1.7026.17263                
NuGet\Install-Package Konnect.Log4NetAlaAppender -Version 1.1.7026.17263                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Konnect.Log4NetAlaAppender" Version="1.1.7026.17263" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Konnect.Log4NetAlaAppender --version 1.1.7026.17263                
#r "nuget: Konnect.Log4NetAlaAppender, 1.1.7026.17263"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Konnect.Log4NetAlaAppender as a Cake Addin
#addin nuget:?package=Konnect.Log4NetAlaAppender&version=1.1.7026.17263

// Install Konnect.Log4NetAlaAppender as a Cake Tool
#tool nuget:?package=Konnect.Log4NetAlaAppender&version=1.1.7026.17263                

Konnect.Log4NetAlaAppender

This is a small, but hopefully a production-grade Log4Net adapter for Azure Log Analytics.

This appender uses BufferedAppenderSkeleton class from Log4Net, which allows this project to be free of concerns such as buffering and flushing.

The log messages are sent using the Log Analytics Data Collector API asynchronously (using Task.Run), but failed attempts are not retried currently.

Captured fields

Currently, Log4net.Core.LoggingEvent objects are mapped to JSON (as required by the Data Collector API) using this code:

var eventsToSend = events.Select(e => new
                {
                    Level = e.Level.Name, 
                    e.LoggerName, 
                    Message = e.RenderedMessage, 
                    e.TimeStamp, 
                    e.Identity,
                    e.ThreadName, 
                    e.UserName,
                    Exception = e.GetExceptionString()
                });
                
var json = JsonConvert.SerializeObject(eventsToSend);

The mapping function is currently not configurable.

Usage

Add this appender description in your Log4Net configuration:

<appender name="AzureLogAppender" type="Konnect.Log4NetAzureLogAnalytics.KnlAlaAppender, Konnect.Log4NetAlaAppender">
  <WorkspaceId>#{YourWorkspaceId}</WorkspaceId>
  <SharedKey>#{YourSharedKey}</SharedKey>
  <LogType>#{YourLogType}</LogType>
</appender>

Please note, that if #{YourLogType} includes a space, it will be stripped - otherwise, ALA API will return an error. Any other whitespace characters are not stripped, so please be careful.

And then, add you can reference this logger from the logger configuration - for example:

<root>
  <level value="INFO" />
  <appender-ref ref="AzureLogAppender" />
</root>

There is a Debug feature, in case there is a problem with connectivity - you can set the EnableDebugLog option in the appender configuration like this:

<appender name="AzureLogAppender" type="Konnect.Log4NetAzureLogAnalytics.KnlAlaAppender, Konnect.Log4NetAlaAppender">
  <WorkspaceId>#{YourWorkspaceId}</WorkspaceId>
  ...
  <EnableDebugLog>true</EnableDebugLog>
</appender>

The debug output will be sent to the Console as well as the Debug output, which can be viewed through tools such as SysInternal's DebugView

If you're trying to troubleshoot this in a release environment, the Debug output will be disabled. You can use the Debug log file feature, like this:

<appender name="AzureLogAppender" type="Konnect.Log4NetAzureLogAnalytics.KnlAlaAppender, Konnect.Log4NetAlaAppender">
  <WorkspaceId>#{YourWorkspaceId}</WorkspaceId>
  ...
  <EnableDebugLog>true</EnableDebugLog>
  <DebugLogFile>C:\temp\ALAAppenderLog.log</DebugLogFile>
</appender>

There is also the master switch, which is enabled by default if the element is missing. This is useful for temporarily stopping logging.

<appender name="AzureLogAppender" type="Konnect.Log4NetAzureLogAnalytics.KnlAlaAppender, Konnect.Log4NetAlaAppender">
  ...
  <IsEnabled>false</IsEnabled>
  ...
</appender>

Product Compatible and additional computed target framework versions.
.NET Framework net471 is compatible.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.7026.17263 620 3/27/2019
1.0.0 666 11/6/2018

Initial release