(1):引用log4net
(2):using log4net;
(3):构造函数中
static CoreLogging()
{
log4net.Config.XmlConfigurator.Configure();
}
(4):private const string UnknownDeviceCategory = "UnknownDevice";
private const string AlarmCategory = "Alarm";
private const string GeneralCategory = "Trace";
private const string ScriptCategory = "Script";
private const string ConfigCategory = "Config";
private const string ScanCategory = "Scan";
private const string UdpCategory = "UdpSender";
private const string MessageMergeCategory = "MessageMerge";
private static ILog traceLog = LogManager.GetLogger(GeneralCategory);
private static ILog scriptLog = LogManager.GetLogger(ScriptCategory);
private static ILog configLog = LogManager.GetLogger(ConfigCategory);
private static ILog scanLog = LogManager.GetLogger(ScanCategory);
private static ILog messageMergeLog = LogManager.GetLogger(MessageMergeCategory);
private static ILog udpLog = LogManager.GetLogger(UdpCategory);
private static ILog alarmLog = LogManager.GetLogger(AlarmCategory);
private static ILog unknownDeviceLog = LogManager.GetLogger(UnknownDeviceCategory);
private static void Log(ILog logger, Object message, Priority priority)
{
switch (priority)
{
case Priority.DEBUG:
logger.Debug(message);
break;
case Priority.INFO:
logger.Info(message);
break;
case Priority.WARN:
logger.Warn(message);
break;
case Priority.ERROR:
logger.Error(message);
break;
case Priority.FATAL:
logger.Fatal(message);
break;
}
}
public static void LogMessageMergeLog(object message, Priority priority)
{
Log(messageMergeLog, message, priority);
}
/// <summary>
/// 通过Udp发送日志
/// </summary>
/// <param name="message">要发送的消息内容</param>
/// <param name="priority">优先级</param>
public static void SendLogViaUdp(object message, Priority priority)
{
Log(udpLog, message, priority);
}
其他的略。
配置文件中需要配置如下
<log4net xmlns="urn:log4net" debug="false">
<appender name="TraceRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log\App\trace" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<param name="StaticLogFileName" value="false" />
<datePattern value="yyyyMMdd'.log'" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<!--日志最大个数-->
<maxSizeRollBackups value="10" />
<!--单个日志的最大容量-->
<maximumFileSize value="100MB" />
<layout type="log4net.Layout.PatternLayout">
<header value="" />
<footer value="" />
<conversionPattern value="%date 线程ID: [%thread] 日志级别: %-5level - 描述: %message%newline" />
</layout>
</appender>
<!-- 用Udp来发送日志 -->
<appender name="UdpAppender" type="log4net.Appender.UdpAppender">
<localPort value="8850" />
<remoteAddress value="127.0.0.1" />
<remotePort value="9833" />
<layout type="log4net.Layout.PatternLayout, log4net">
<conversionPattern value="%message%newline" />
</layout>
</appender>
其他略
下面为各个动作。Trace类型
<logger name="Trace">
<level value="ALL" />
<appender-ref ref="TraceRollingFileAppender" />
<appender-ref ref="ColoredConsoleAppender" />
</logger>
<logger name="Script">
<level value="ALL" />
<appender-ref ref="ScriptRollingFileAppender" />
<appender-ref ref="ColoredConsoleAppender" />
</logger>
<logger name="Config">
<level value="ALL" />
<appender-ref ref="ConfigRollingFileAppender" />
<appender-ref ref="ColoredConsoleAppender" />
</logger>
<logger name="Scan">
<level value="ALL" />
<appender-ref ref="ScanRollingFileAppender" />
<appender-ref ref="ColoredConsoleAppender" />
</logger>
<logger name="MessageMerge">
<level value="ALL" />
<appender-ref ref="ScanRollingFileAppender" />
<appender-ref ref="ColoredConsoleAppender" />
</logger>
<logger name="UdpSender">
<level value="ALL" />
<appender-ref ref="UdpAppender" />
<!--<appender-ref ref="ColoredConsoleAppender"/>-->
</logger>
<logger name="Alarm">
<level value="ALL" />
<appender-ref ref="AlarmAppender" />
</logger>
<logger name="UnknownDevice">
<level value="ALL" />
<appender-ref ref="UnknownDeviceAppender" />
</logger>