c# - Common.Logging 支持记录到文件。

标签 c# .net logging common.logging

我正在查看通用日志记录的文档,但没有注意到除了利用其他日志记录技术(如 Log4Net、Enterprise library、Nlog 等)之外对写入日志文件的任何支持。我只是想知道是否有人知道一种配置 Common.Logging 以写入文件的方法,或者如果我必须回退到包含在 common.logging 中的另一种日志记录技术。

最佳答案

您选择一个特定的日志记录框架与 Common Logging 一起使用,并配置那个以将日志记录到文件中。例如,如果您想将 log4net 与 Common Logging 一起使用,您可能有一个如下所示的配置文件:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging"/>
    </sectionGroup>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
        <arg key="configType" value="INLINE"/>
      </factoryAdapter>
    </logging>
  </common>
  <log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender" >
  <param name="File" value="log.txt" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
  </layout>
</appender>

<root>
  <level value="DEBUG" />
  <appender-ref ref="FileAppender" />
</root>
  </log4net>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-2.1.2.0" newVersion="2.1.2.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

关于c# - Common.Logging 支持记录到文件。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16406296/

相关文章:

c# - 我可以在 Quartz.NET 中自己持久化触发器吗?

ruby-on-rails - Rails 开发是描述并显示所有请求的表格,如何关闭它?

logging - 事件源和记录/重放命令

c# - 如何在 ASP.NET MVC 中处理同一窗体上的编辑和删除按钮?

c# - 错误提示:找不到方法:“无效RestSharp.RestClient.set_BaseUrl(System.String)”。在特维里奥沙普

c# - 寻找替代 for 循环的 linq 解决方案

c# - 通过文件共享、用户身份验证通过网络复制文件

asp.net - 停止掩码附加 '0'

c# - 动态调用 DLL 中的方法

.net - ASP.net : How to log from within CreateHostBuilder()?