GithubHelp home page GithubHelp logo

josonjiang / log4netadonetappender Goto Github PK

View Code? Open in Web Editor NEW

This project forked from microknights/log4netadonetappender

0.0 1.0 0.0 92 KB

.Net Standard 1.3-2.1 AdoNetAppender

License: Apache License 2.0

C# 98.96% Batchfile 1.04%

log4netadonetappender's Introduction

Log4NetAdoNetAppender

.Net Standard 2.0 AdoNetAppender

A late night fix since it do not exists for .net core for some odd reason. This project will only exists until the official logging.apache.org project is releasing their own AdoNetAppender within the log4net package.

How to setup

You log4net.config AdoNetAppender configuration (Read about use of Microsoft.Data.SqlClient below!)

<log4net>
  <appender name="AdoNetAppender" type="MicroKnights.Logging.AdoNetAppender, MicroKnights.Log4NetAdoNetAppender">
    <bufferSize value="1" />
    <connectionType value="System.Data.SqlClient.SqlConnection,System.Data.SqlClient,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089" />
    <connectionStringName value="log4net" />
    <connectionStringFile value="connectionstrings.json" />
    <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
    <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
    </parameter>
    <parameter>
        <parameterName value="@thread" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%thread" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@log_level" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%level" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@logger" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%logger" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@message" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%message" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@exception" />
        <dbType value="String" />
        <size value="2000" />
        <layout type="log4net.Layout.ExceptionLayout" />
    </parameter>
  </appender>

With 2 changes, you are up and running in your .net core project.

First you must change/replace the type and assembly name, from this:

<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">

to this:

<appender name="AdoNetAppender" type="MicroKnights.Logging.AdoNetAppender, MicroKnights.Log4NetAdoNetAppender">

Secondly you must specify the configurationfile where your connectionstrings are defined. A new property in the configuration is added - called ConnectionStringFile. It will then locate the connectionstring by using ConnectionStringName.

Set connection at runtime

Instead of getting the connectionstring through a file, you can set it directly at runtime.

Take a peek at the Log4NetHelper project, from where this is possible.

NuGet Package

PM> Install-Package MicroKnights.Log4NetAdoNetAppender

Note for .Net Core 3 (up til .Net standard 2.0)

Due to a fix in the Type loader, the configuration has a small change in the connectionType property.

Was previously: <connectionType value="System.Data.SqlClient.SqlConnection,System.Data,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089" />

and is now:

<connectionType value="System.Data.SqlClient.SqlConnection,System.Data.SqlClient,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089" />

this change also works backwards, for versions before .Net standard 2.0

Note for .Net Core 3.1+ (from .Net standard 2.1)

Microsoft has changed path for the SqlClient library, and we are following. Previously it was System.Data.SqlClient and now it is Microsoft.Data.SqlClient.

So we must reflect this change, and this is done on the connectionType property.

Was previously: <connectionType value="System.Data.SqlClient.SqlConnection,System.Data.SqlClient,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089" />

and is now:

<connectionType value="Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient, Version=1.0.0.0,Culture=neutral,PublicKeyToken=23ec7fc2d6eaa4a5"/>

this change do not work backwards, only for version .Net Standard 2.1 and forward

log4netadonetappender's People

Contributors

microknights avatar franklnielsen avatar souzaemarcal avatar

Watchers

James Cloos 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.