Lofka.Log4net 0.0.8

dotnet add package Lofka.Log4net --version 0.0.8
NuGet\Install-Package Lofka.Log4net -Version 0.0.8
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="Lofka.Log4net" Version="0.0.8" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Lofka.Log4net --version 0.0.8
#r "nuget: Lofka.Log4net, 0.0.8"
#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 Lofka.Log4net as a Cake Addin
#addin nuget:?package=Lofka.Log4net&version=0.0.8

// Install Lofka.Log4net as a Cake Tool
#tool nuget:?package=Lofka.Log4net&version=0.0.8

Lofka.Dotnet.Log4net

本项目是基于 log4net 2.0.8版本开发,兼容.net Framework 4.5+ 和.net core 相信服务器端应用程序一般都应该升级到.net Framework 4.5以上版本了。 其他版本只有等以后有时间再来实现了.

项目包含两个适配器 HttpAppenderHttpAysncAppender

HttpAppender

HttpAppender适配器通常用于日志产生不频繁,且日志量比较小的情况。基本上产生日志就会把日志内容推送至lofka日志服务器,基本不存在延迟的情况(应用与服务器之间网络状况比较差,暂不在考虑范围emmmmm~)。

配置

和log4net官方配置很相像,只是需要针对HttpAppender适配器做出额外配置。主要集中在 appeder 节点上,下面是一个简单的HttpAppender配置:

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <appender name="HttpAppender" type="Lofka.Dotnet.Log4net.HttpAppender,Lofka.Log4net">
    
    <param name="Application" value="Example/Log4net/HttpAppender"/>
    
    <param name="ServerHost" value="http://lofka.example.com"/>
    
    <param name="Target" value="/lofka/service/push"/>
    
    <param name="IsCompress" value="false"/>
  </appender>
  <root>
    <level value="ALL" />
    <appender-ref ref="HttpAppender" />
  </root>
</log4net>

无需layout配置,lofka统一管理日志存储格式。(至于个性化的显示格式,你可以根据需要参考lofka日志格式自行修改。)

使用方法

以下是一个简单的示例代码,主要作用是输出一句Info级别的日志,以及异常日志记录。

class Program
{
    static void Main(string[] args)
    {
        var configFile = new FileInfo("log4net.config");
        var assembly = Assembly.GetAssembly(typeof(Lofka.Dotnet.Log4net.HttpAppender));//加载Lofka.Dotnet.Log4net应用程序集
        var repository = LogManager.GetRepository(assembly);
        XmlConfigurator.Configure(repository, configFile);//初始化配置
        var log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        log.Info("Hello,this is Example");
        try
        {
            var val = int.Parse("abc");
        }
        catch (FormatException ex)
        {
            log.Error("Error!", ex);
        }
        Console.ReadLine();
    }
}

HttpAysncAppender

HttpAysncAppender 是一个异步的适配器,特点是,收到日志以后不会立即向lofka服务器推送日志。而是先将日志信息存放在日志缓存中,当积攒到指定数量或到达指定的时间间隔,才会将缓存中的日志推送至lofka服务器,并且默认会根据推送内容大小(目前写死1024字节)自动进行压缩。

特别适用于日志内容比较大,并且频繁产生的情况。

配置

以下是一个简单的HttpAsyncAppender配置

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <appender name="HttpAsyncAppender" type="Lofka.Dotnet.Log4net.HttpAsyncAppender,Lofka.Log4net">
    
    <param name="Application" value="Example/Log4net/HttpAsyncAppender"/>
    
    <param name="ServerHost" value="http://lofka.example.com"/>
    
    <param name="Target" value="/lofka/service/push/batch"/>
    
    <param name="CompressTarget" value="/lofka/service/push/batch/zip"/>
    
    <param name="AutoCompress" value="true"/>
    
    <param name="MaxSize" value="30"/>
    
    <param name="Interval" value="10000"/>
  </appender>
  <root>
    <level value="ALL" />
    <appender-ref ref="HttpAsyncAppender" />
  </root>
</log4net>

不建议对TargetCompressTarget的配置进行修改,除非你修改了lofka的核心代码。

使用方法

HttpAppender的示例程序一样,只是为了达到批量推送日志的目的,在原有的输出Info级别和异常日志的基础上增加了一个90次的循环。

 class Program
 {
     static void Main(string[] args)
     {
         Thread.CurrentThread.Name = "main";
         var configFile = new FileInfo("log4net.config");
         var assembly = Assembly.GetAssembly(typeof(Lofka.Dotnet.Log4net.HttpAsyncAppender));//加载Lofka.Dotnet.Log4net应用程序集
         var repository = LogManager.GetRepository(assembly);
         XmlConfigurator.Configure(repository, configFile);//初始化配置
         var log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
         for (int i = 0; i < 90; i++)
         {
             log.Info("Hello,this is Example");
             try
             {
                 var val = int.Parse("abc");
             }
             catch (FormatException ex)
             {
                 log.Error(ex.ToString(), ex);
             } 
         }
         Console.ReadLine();
     }
 }

特别说明

我发现使用 HttpAppenderHttpAysncAppender 过程中,需要加载自定义配置文件,而加载配置文件以需要把Lofka.Dotnet.Log4net加载进系统,不然有时候会报log4net无法加载应用程序集Lofka.Dotnet.Log4net,并且通过调试发现无法进入自定义appender的Append方法。这个问题曾困扰我很久=====( ̄▽ ̄*)b 可以参考如下代码来加载Lofka.Dotnet.Log4net应用程序集

HttpAppender
var configFile = new FileInfo("log4net.config");
var assembly = Assembly.GetAssembly(typeof(Lofka.Dotnet.Log4net.HttpAppender));//加载Lofka.Dotnet.Log4net应用程序集
var repository = LogManager.GetRepository(assembly);
XmlConfigurator.Configure(repository, configFile);//初始化配置
HttpAysncAppender
var configFile = new FileInfo("log4net.config");
var assembly = Assembly.GetAssembly(typeof(Lofka.Dotnet.Log4net.HttpAsyncAppender));//加载Lofka.Dotnet.Log4net应用程序集
var repository = LogManager.GetRepository(assembly);
XmlConfigurator.Configure(repository, configFile);//初始化配置

写在最后

我觉得这两个Appender 各有优缺点,HttpAysncAppender 如果放在应用程序退出的时候输出日志,那么很有可能会丢失日志,HttpAppender的网络开销又太大,不适合大规模使用,所以理论上 HttpAppender 和 HttpAysncAppender 应该配合使用,本项目理论上也支持配合使用,但我个人没试过。━┳━ ━┳━ 后面再写一个配合使用的示例.

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 net40 is compatible.  net403 was computed.  net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  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
0.0.8 1,288 12/11/2018
0.0.7 1,058 12/7/2018
0.0.5 1,224 12/6/2018

1、HttpAsyncAppender MaxSize触发向lofka服务器推送改成在线程内判断;
2、增加单次向lofka服务器推送条数约束;