WeihanLi.Common.Logging.Log4Net 1.0.53-preview-20220707-145349

Prefix Reserved
This is a prerelease version of WeihanLi.Common.Logging.Log4Net.
There is a newer version of this package available.
See the version list below for details.
dotnet add package WeihanLi.Common.Logging.Log4Net --version 1.0.53-preview-20220707-145349                
NuGet\Install-Package WeihanLi.Common.Logging.Log4Net -Version 1.0.53-preview-20220707-145349                
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.53-preview-20220707-145349" />                
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.53-preview-20220707-145349                
#r "nuget: WeihanLi.Common.Logging.Log4Net, 1.0.53-preview-20220707-145349"                
#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.53-preview-20220707-145349&prerelease

// Install WeihanLi.Common.Logging.Log4Net as a Cake Tool
#tool nuget:?package=WeihanLi.Common.Logging.Log4Net&version=1.0.53-preview-20220707-145349&prerelease                

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 142 7/28/2023
1.0.60-preview-20230723-122007 138 7/23/2023
1.0.60-preview-20230715-064515 133 7/15/2023
1.0.59 984 3/14/2023
1.0.58 345 11/26/2022
1.0.57 373 11/20/2022
1.0.56 399 11/11/2022
1.0.56-preview-20221110-145231 152 11/10/2022
1.0.55 414 10/26/2022
1.0.54 461 10/12/2022
1.0.54-preview-20220927-012727 168 9/27/2022
1.0.53 524 7/17/2022
1.0.53-preview-20220709-025158 174 7/9/2022
1.0.53-preview-20220707-145349 161 7/7/2022
1.0.52 467 5/28/2022
1.0.51 500 4/9/2022
1.0.50 524 3/16/2022
1.0.49 441 3/13/2022
1.0.49-preview-20220312-180433 170 3/12/2022
1.0.49-preview-20220309-012617 168 3/9/2022
1.0.49-preview-20220306-151500 169 3/6/2022
1.0.48 354 1/8/2022
1.0.48-preview-20211220-105237 195 12/20/2021
1.0.48-preview-20211212-110315 186 12/12/2021
1.0.47 391 11/28/2021
1.0.47-preview-20211009-134935 226 10/9/2021
1.0.46 490 3/20/2021
1.0.46-preview-20210320-094828 282 3/20/2021
1.0.46-preview-20210320-090449 291 3/20/2021
1.0.46-preview-20210320-065305 330 3/20/2021
1.0.46-preview-20210313-212124 241 3/13/2021
1.0.45 420 2/28/2021
1.0.44 376 2/13/2021
1.0.44-preview-20210201-164451 237 2/1/2021
1.0.44-preview-20210131-152307 256 1/31/2021
1.0.44-preview-20210131-041429 235 1/31/2021
1.0.43 620 11/14/2020
1.0.43-preview-20201108-152412 384 11/8/2020
1.0.42 488 10/18/2020
1.0.42-preview-20200920-113000 375 9/20/2020
1.0.41 620 8/8/2020
1.0.41-preview-20200804-090048 320 8/4/2020
1.0.40 537 7/15/2020
1.0.40-preview-20200713-113453 337 7/13/2020
1.0.40-preview-20200625-063932 351 6/25/2020
1.0.39 555 6/13/2020
1.0.39-preview-20200610-070004 343 6/10/2020
1.0.39-preview-20200603-023614 380 6/3/2020
1.0.38 524 5/22/2020
1.0.38-preview-20200520-154642 358 5/20/2020
1.0.38-preview-20200519-171358 351 5/19/2020
1.0.37 551 5/17/2020
1.0.37-preview-20200517-083059 365 5/17/2020
1.0.37-preview-20200516-074403 351 5/16/2020
1.0.37-preview-20200514-094816 330 5/14/2020
1.0.37-preview-20200514-042225 328 5/14/2020
1.0.36 480 5/12/2020
1.0.36-preview-20200511-165228 343 5/11/2020
1.0.36-preview-20200511-151124 356 5/11/2020
1.0.36-preview-20200507-074046 343 5/7/2020
1.0.35 472 5/1/2020
1.0.35-preview-20200429-180449 391 4/29/2020
1.0.35-preview-20200426-133934 346 4/26/2020
1.0.35-preview-20200425-165753 338 4/25/2020
1.0.35-preview-20200425-035111 329 4/25/2020
1.0.34 516 4/19/2020
1.0.28 986 1/1/2020
1.0.27 598 12/31/2019
1.0.27-preview-20191230-051305 408 12/30/2019
1.0.26 666 11/28/2019
1.0.23.8 952 8/3/2019
1.0.23.8-preview-20190802-1... 400 8/2/2019
1.0.23.7 556 7/23/2019
1.0.22.6 757 7/16/2019
1.0.17 1,190 4/27/2019
1.0.13 870 3/14/2019