Code Monkey home page Code Monkey logo

konnect.log4netalaappender's Introduction

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>

konnect.log4netalaappender's People

Contributors

jkkonnect avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.