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 141 7/28/2023
1.0.60-preview-20230723-122007 137 7/23/2023
1.0.60-preview-20230715-064515 132 7/15/2023
1.0.59 981 3/14/2023
1.0.58 344 11/26/2022
1.0.57 372 11/20/2022
1.0.56 398 11/11/2022
1.0.56-preview-20221110-145231 151 11/10/2022
1.0.55 413 10/26/2022
1.0.54 460 10/12/2022
1.0.54-preview-20220927-012727 167 9/27/2022
1.0.53 521 7/17/2022
1.0.53-preview-20220709-025158 173 7/9/2022
1.0.53-preview-20220707-145349 160 7/7/2022
1.0.52 466 5/28/2022
1.0.51 499 4/9/2022
1.0.50 523 3/16/2022
1.0.49 440 3/13/2022
1.0.49-preview-20220312-180433 169 3/12/2022
1.0.49-preview-20220309-012617 167 3/9/2022
1.0.49-preview-20220306-151500 168 3/6/2022
1.0.48 353 1/8/2022
1.0.48-preview-20211220-105237 194 12/20/2021
1.0.48-preview-20211212-110315 185 12/12/2021
1.0.47 390 11/28/2021
1.0.47-preview-20211009-134935 225 10/9/2021
1.0.46 489 3/20/2021
1.0.46-preview-20210320-094828 281 3/20/2021
1.0.46-preview-20210320-090449 290 3/20/2021
1.0.46-preview-20210320-065305 329 3/20/2021
1.0.46-preview-20210313-212124 240 3/13/2021
1.0.45 419 2/28/2021
1.0.44 375 2/13/2021
1.0.44-preview-20210201-164451 236 2/1/2021
1.0.44-preview-20210131-152307 255 1/31/2021
1.0.44-preview-20210131-041429 232 1/31/2021
1.0.43 619 11/14/2020
1.0.43-preview-20201108-152412 383 11/8/2020
1.0.42 487 10/18/2020
1.0.42-preview-20200920-113000 374 9/20/2020
1.0.41 619 8/8/2020
1.0.41-preview-20200804-090048 319 8/4/2020
1.0.40 536 7/15/2020
1.0.40-preview-20200713-113453 336 7/13/2020
1.0.40-preview-20200625-063932 350 6/25/2020
1.0.39 554 6/13/2020
1.0.39-preview-20200610-070004 342 6/10/2020
1.0.39-preview-20200603-023614 379 6/3/2020
1.0.38 523 5/22/2020
1.0.38-preview-20200520-154642 357 5/20/2020
1.0.38-preview-20200519-171358 350 5/19/2020
1.0.37 550 5/17/2020
1.0.37-preview-20200517-083059 364 5/17/2020
1.0.37-preview-20200516-074403 350 5/16/2020
1.0.37-preview-20200514-094816 329 5/14/2020
1.0.37-preview-20200514-042225 327 5/14/2020
1.0.36 479 5/12/2020
1.0.36-preview-20200511-165228 342 5/11/2020
1.0.36-preview-20200511-151124 355 5/11/2020
1.0.36-preview-20200507-074046 340 5/7/2020
1.0.35 471 5/1/2020
1.0.35-preview-20200429-180449 390 4/29/2020
1.0.35-preview-20200426-133934 345 4/26/2020
1.0.35-preview-20200425-165753 337 4/25/2020
1.0.35-preview-20200425-035111 328 4/25/2020
1.0.34 515 4/19/2020
1.0.28 975 1/1/2020
1.0.27 597 12/31/2019
1.0.27-preview-20191230-051305 407 12/30/2019
1.0.26 663 11/28/2019
1.0.23.8 951 8/3/2019
1.0.23.8-preview-20190802-1... 398 8/2/2019
1.0.23.7 555 7/23/2019
1.0.22.6 756 7/16/2019
1.0.17 1,189 4/27/2019
1.0.13 869 3/14/2019