common.logging是基于java语言的一个日志门面框架,是Apache基金会下的commons项目下的一个子项目,让你的日志脱离具体的实现。
Commong.Logging是.NET下的日志门面框架,它是基于C#语言的移植。并且做了一些基于Windows的扩展。比如ETW(Windows事件跟踪)等独特的日志记录和收集方式。关于如何收集ETW可以参考https://www.cnblogs.com/artech/p/logging-via-etw.html 。
今天主要讲讲MultipleLogger的配置,首先我们使用nuget安装相关的软件包,最开始的时候我是自己尝试,一直没有配置正确,后来在源码的tests目录下,找到了单元测试的配置文件才搞明白。
App.config见下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging"/>
<section name="logging.multipleLoggers" type="Common.Logging.MultipleLogger.ConfigurationSectionHandler, Common.Logging.MultipleLogger"/>
</sectionGroup>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.MultipleLogger.MultiLoggerFactoryAdapter, Common.Logging.MultipleLogger">
<arg key="MeaninglessKey" value="MeaninglessValue"/>
</factoryAdapter>
</logging>
<logging.multipleLoggers>
<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1215">
<!--FILE
FILE-WATCH
INLINE
EXTERNAL-->
<!--
-->
<!--<arg key="configType" value="INLINE" />-->
<arg key="configType" value="FILE-WATCH" />
<arg key="configFile" value="~/log4net.config" />
<!--log4net使用独立的配置文件-->
</factoryAdapter>
<factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog4412">
<arg key="configType" value="FILE" />
<arg key="configFile" value="~/NLog.config" />
</factoryAdapter>
<factoryAdapter type="Common.Logging.ETW.ETWLoggerFactoryAdapter,Common.Logging.ETWLogger">
<arg key="level" value="DEBUG"/>
<!--<arg key="commonLoggingEventSourceType" value="Common.Logging.ETW.CommonLoggingEventSource,Common.Logging.ETWLogger"/>-->
<arg key="permitDuplicateEventSourceRegistration" value="false"/>
</factoryAdapter>
</logging.multipleLoggers>
</common>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
在配置Section时,除了标准的loggin外,记得把Multe的处理节点也要配置上,如下:
<section name="logging.multipleLoggers" type="Common.Logging.MultipleLogger.ConfigurationSectionHandler, Common.Logging.MultipleLogger"/>
记住,这两个一个都不要少。
然后配置一个log4net.config配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<!--<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" />
</layout>
</appender>-->
<appender name="InfoRollingLogFileAppender" type="log4net.Appender.RollingFileAppender, log4net">
<param name="LockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
<param name="File" value="logs/" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="'Info-'yyyy-MM-dd.'log'" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="5MB" />
<param name="RollingStyle" value="Date" />
<!--Size ==- Date-->
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG"/>
<levelMax value="WARN"/>
</filter>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender, log4net">
<param name="LockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
<param name="File" value="logs/" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="'Error-'yyyy-MM-dd.'log'" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="5MB" />
<param name="RollingStyle" value="Date" />
<!--Size ==- Date-->
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR"/>
<levelMax value="FATAL"/>
</filter>
</appender>
<appender name="DefaultRollingLogFileAppender" type="log4net.Appender.RollingFileAppender, log4net">
<param name="LockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
<param name="File" value="logs/" /><!--文件保存的路径-->
<param name="StaticLogFileName" value="false" /><!--文件名动态生成-->
<param name="DatePattern" value="'Default-'yyyy-MM-dd.'log'" /><!--文件名命名规则-->
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="5MB" /><!--文件超过5M将创建新文件-->
<param name="RollingStyle" value="Date" />
<!--Size ==- Date-->
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /><!--日志内容格式-->
</layout>
<filter type="log4net.Filter.LevelRangeFilter"><!--日志级别过滤器:此附加器只附加指定级别的日志-->
<levelMin value="DEBUG"/>
<levelMax value="FATAL"/>
</filter>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref="InfoRollingLogFileAppender" />
</root>
<logger name="MyApp.DataAccessLayer">
<level value="DEBUG" />
</logger>
<!-- ALL DEBUG INFO WARN ERROR FATAL OFF [DEBUG < INFO < WARN < ERROR < FATAL] 大范围 到 小范围-->
<logger name="LogDemoWeb._Default" debug="false">
<level value="ALL" />
<appender-ref ref="DefaultRollingLogFileAppender" />
</logger>
</log4net>
再来一个NLog的配置文件NLog.config:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<variable name="variable1" value="${newline}date: ${date}${newline}level: ${level}${newline}logger: ${logger}${newline}machinename: ${machinename}
${newline}message: ${message}${newline}appdomain: ${appdomain}${newline}assembly-version: ${assembly-version}${newline}basedir: ${basedir}
${newline}callsite: ${callsite}${newline}callsite-linenumber: ${callsite-linenumber}${newline}counter: ${counter}${newline}nlogdir: ${nlogdir}
${newline}processid: ${processid}${newline}processname: ${processname}${newline}specialfolder: ${specialfolder}
${newline}stacktrace: ${stacktrace}${newline}-----------------------------------------------------------" />
<targets>
<target name="log_file" xsi:type="File" fileName="${basedir}/LogInformation/${level}_${shortdate}.txt" layout="${variable1}" />
</targets>
<rules>
<logger name="*" writeTo="log_file" />
</rules>
</nlog>
虽然这个多日志输出的Adapter作用不大,但我们也不得不佩服这些开源人的想法。