Common Logging 项目教程

Common Logging 项目教程

common-logging A portable logging abstraction for .NET common-logging 项目地址: https://gitcode.com/gh_mirrors/co/common-logging

1. 项目介绍

Common Logging 是一个为 .NET 平台提供的可移植日志抽象库。它允许开发者在不同的日志实现之间轻松切换,支持多种日志框架,如 log4net、NLog、Microsoft Enterprise Library 日志、Microsoft Application Insights、Microsoft Event Tracing for Windows 和 Serilog。Common Logging 还提供了一组基类,使得集成任何日志系统变得非常简单。

2. 项目快速启动

2.1 安装 Common Logging

首先,通过 NuGet 安装 Common Logging 包:

PM> Install-Package Common.Logging

2.2 配置 Common Logging

在你的应用程序配置文件(如 app.configweb.config)中添加以下配置:

<configuration>
  <configSections>
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>
  </configSections>
  <common>
    <logging>
      <factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging">
        <arg key="level" value="INFO" />
        <arg key="showLogName" value="true" />
        <arg key="showDateTime" value="true" />
        <arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:fff" />
      </factoryAdapter>
    </logging>
  </common>
</configuration>

2.3 使用 Common Logging

在你的代码中使用 Common Logging 进行日志记录:

using Common.Logging;

public class MyClass
{
    private static readonly ILog log = LogManager.GetLogger<MyClass>();

    public void DoSomething()
    {
        log.Info("This is an informational message.");
        log.Error("This is an error message.");
    }
}

3. 应用案例和最佳实践

3.1 集成 NLog

如果你使用 NLog 作为日志框架,可以通过 NuGet 安装相应的适配器包:

PM> Install-Package Common.Logging.NLog41

然后在配置文件中添加 NLog 的配置:

<common>
  <logging>
    <factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog41">
      <arg key="configType" value="INLINE" />
    </factoryAdapter>
  </logging>
</common>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <!-- NLog 配置 -->
</nlog>

3.2 集成 Microsoft Application Insights

如果你使用 Microsoft Application Insights,可以通过 NuGet 安装相应的适配器包:

PM> Install-Package Microsoft.ApplicationInsights -Version 0.17.0

然后在配置文件中添加 Application Insights 的配置:

<common>
  <logging>
    <factoryAdapter type="Common.Logging.ApplicationInsights.ApplicationInsightsLoggerFactoryAdapter, Common.Logging.ApplicationInsights">
      <arg key="InstrumentationKey" value="[YOUR APPLICATION INSIGHTS INSTRUMENTATION KEY]" />
    </factoryAdapter>
  </logging>
</common>

4. 典型生态项目

4.1 log4net

log4net 是一个广泛使用的日志框架,Common Logging 提供了对 log4net 的集成支持。你可以通过 NuGet 安装 Common.Logging.Log4Net 包来使用。

4.2 Serilog

Serilog 是一个现代的结构化日志框架,Common Logging 也提供了对 Serilog 的集成支持。你可以通过 NuGet 安装 Common.Logging.Serilog 包来使用。

4.3 Microsoft Enterprise Library

Microsoft Enterprise Library 是一个企业级应用程序块集合,Common Logging 提供了对其日志模块的集成支持。你可以通过 NuGet 安装 Common.Logging.EntLib 包来使用。

通过这些集成,Common Logging 可以帮助你在不同的日志框架之间无缝切换,提高开发效率和代码的可维护性。

common-logging A portable logging abstraction for .NET common-logging 项目地址: https://gitcode.com/gh_mirrors/co/common-logging

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明俪钧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值