WeihanLi.Common.Logging.Log4Net 1.0.59

Prefix Reserved
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package WeihanLi.Common.Logging.Log4Net --version 1.0.59                
NuGet\Install-Package WeihanLi.Common.Logging.Log4Net -Version 1.0.59                
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="WeihanLi.Common.Logging.Log4Net" Version="1.0.59" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WeihanLi.Common.Logging.Log4Net --version 1.0.59                
#r "nuget: WeihanLi.Common.Logging.Log4Net, 1.0.59"                
#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 WeihanLi.Common.Logging.Log4Net as a Cake Addin
#addin nuget:?package=WeihanLi.Common.Logging.Log4Net&version=1.0.59

// Install WeihanLi.Common.Logging.Log4Net as a Cake Tool
#tool nuget:?package=WeihanLi.Common.Logging.Log4Net&version=1.0.59                

WeihanLi.Common.Logging.Log4Net WeihanLi.Common.Logging.Log4Net

Intro

  • log4net extensions

    • log4net ElasticSearchAppender
    • Log4NetHelper
  • Log4NetLoggerProviderfor Microsoft.Extensions.Logging

  • Log4NetLogHelperProvider for WeihanLi.Common.Logging

Use log4net only

  1. log4net init

    Log4NetHelper.LogInit(); // log4net.config for default
    
    Log4NetHelper.LogInit(log4netConfigFilePath);
    
  2. GetLogger

    var genericLogger = Log4NetHelper.GetLogger<ClassName>();
    
    var typeLogger = Log4NetHelper.GetLogger(typeof(Program));
    
    var logger = Log4NetHelper.GetLogger("LoggerName");
    
  3. Log message

    logger.Debug(msg);
    logger.Info(msg);
    logger.Info(msg, exception);
    logger.Warn(msg);
    logger.Warn(msg, exception);
    logger.Error(msg);
    logger.Error(msg, exception);
    logger.Fatal(msg);
    logger.Fatal(msg, exception);
    
  4. log4net config sample

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="./Log/systemLog.%date{yyyy-MM-dd}.log" />
      <appendToFile value="true" />
      <encoding value="unicodeFFFE" />
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMdd" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
      </layout>
    </appender>
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="100" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data" />
      <connectionString value="data source=.;initial catalog=WebLog;integrated security=false;persist security info=True;User ID=[***];Password=[***]" />
      <commandText value="INSERT INTO [dbo].[TestLog]([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>
      <filter type="log4net.Filter.LevelRangeFilter">
        
        <param name="LevelMin" value="WARN" />
      </filter>
    </appender>
    <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <appender name="AspNetTraceAppender" type="log4net.Appender.AspNetTraceAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
      </layout>
    </appender>
    <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
      <mapping>
        <level value="ERROR" />
        <foreColor value="White" />
        <backColor value="Red, HighIntensity" />
      </mapping>
      <mapping>
        <level value="DEBUG" />
        <backColor value="Green" />
      </mapping>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
      </layout>
    </appender>
    <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
      <authentication value="Basic" />
      <username value="ben121011@126.com" />
      <password value="*******" />
      <to value="w***@outlook.com" />
      <from value="ben121011@126.com" />
      <subject value="logging message test" />
      <smtpHost value="smtp.126.com" />
      <bufferSize value="512" />
      
      <lossy value="true" />
      
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="ERROR" />
        <levelMax value="FATAL" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline %date  %newline [%thread] %newline %-5level %logger  %newline - %message" />
      </layout>
    </appender>
    <appender name="ElasticSearchAppender" type="WeihanLi.Common.Logging.Log4Net.ElasticSearchAppender, WeihanLi.Common.Logging.Log4Net">
      <bufferSize value="2" />
      <ElasticSearchUrl value="http://localhost:9200" />
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="TraceAppender" />
      <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="RollingLogFileAppender" />
      
    </root>
  </log4net>
</configuration>

use Log4NetLoggerProvider

ILoggerFactory loggerFactory = new LoggerFactory();
loggerFactory.AddLog4Net(); // loggerFactory.AddLog4Net(log4netConfigFilePath);

you can config in asp.net core use code like below in your Startup:

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddLog4Net();

            // ...
        }

use Log4NetLogHelperProvider

LogHelper.AddLogProvider(new ILogHelperProvider[] {
                new WeihanLi.Common.Logging.Log4Net.Log4NetLogHelperProvider()
            });
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.0.60-preview-20230728-022632 146 7/28/2023
1.0.60-preview-20230723-122007 140 7/23/2023
1.0.60-preview-20230715-064515 135 7/15/2023
1.0.59 1,005 3/14/2023
1.0.58 349 11/26/2022
1.0.57 377 11/20/2022
1.0.56 403 11/11/2022
1.0.56-preview-20221110-145231 154 11/10/2022
1.0.55 418 10/26/2022
1.0.54 465 10/12/2022
1.0.54-preview-20220927-012727 170 9/27/2022
1.0.53 536 7/17/2022
1.0.53-preview-20220709-025158 176 7/9/2022
1.0.53-preview-20220707-145349 163 7/7/2022
1.0.52 473 5/28/2022
1.0.51 506 4/9/2022
1.0.50 530 3/16/2022
1.0.49 447 3/13/2022
1.0.49-preview-20220312-180433 172 3/12/2022
1.0.49-preview-20220309-012617 170 3/9/2022
1.0.49-preview-20220306-151500 171 3/6/2022
1.0.48 360 1/8/2022
1.0.48-preview-20211220-105237 197 12/20/2021
1.0.48-preview-20211212-110315 188 12/12/2021
1.0.47 397 11/28/2021
1.0.47-preview-20211009-134935 228 10/9/2021
1.0.46 496 3/20/2021
1.0.46-preview-20210320-094828 284 3/20/2021
1.0.46-preview-20210320-090449 295 3/20/2021
1.0.46-preview-20210320-065305 332 3/20/2021
1.0.46-preview-20210313-212124 243 3/13/2021
1.0.45 426 2/28/2021
1.0.44 382 2/13/2021
1.0.44-preview-20210201-164451 239 2/1/2021
1.0.44-preview-20210131-152307 258 1/31/2021
1.0.44-preview-20210131-041429 237 1/31/2021
1.0.43 628 11/14/2020
1.0.43-preview-20201108-152412 386 11/8/2020
1.0.42 494 10/18/2020
1.0.42-preview-20200920-113000 377 9/20/2020
1.0.41 626 8/8/2020
1.0.41-preview-20200804-090048 322 8/4/2020
1.0.40 543 7/15/2020
1.0.40-preview-20200713-113453 339 7/13/2020
1.0.40-preview-20200625-063932 353 6/25/2020
1.0.39 561 6/13/2020
1.0.39-preview-20200610-070004 345 6/10/2020
1.0.39-preview-20200603-023614 382 6/3/2020
1.0.38 530 5/22/2020
1.0.38-preview-20200520-154642 360 5/20/2020
1.0.38-preview-20200519-171358 353 5/19/2020
1.0.37 557 5/17/2020
1.0.37-preview-20200517-083059 367 5/17/2020
1.0.37-preview-20200516-074403 353 5/16/2020
1.0.37-preview-20200514-094816 332 5/14/2020
1.0.37-preview-20200514-042225 332 5/14/2020
1.0.36 486 5/12/2020
1.0.36-preview-20200511-165228 345 5/11/2020
1.0.36-preview-20200511-151124 360 5/11/2020
1.0.36-preview-20200507-074046 347 5/7/2020
1.0.35 480 5/1/2020
1.0.35-preview-20200429-180449 393 4/29/2020
1.0.35-preview-20200426-133934 348 4/26/2020
1.0.35-preview-20200425-165753 340 4/25/2020
1.0.35-preview-20200425-035111 331 4/25/2020
1.0.34 522 4/19/2020
1.0.28 1,027 1/1/2020
1.0.27 604 12/31/2019
1.0.27-preview-20191230-051305 410 12/30/2019
1.0.26 674 11/28/2019
1.0.23.8 958 8/3/2019
1.0.23.8-preview-20190802-1... 402 8/2/2019
1.0.23.7 562 7/23/2019
1.0.22.6 763 7/16/2019
1.0.17 1,196 4/27/2019
1.0.13 875 3/14/2019