在将log4net输出到UdpAppender会出现乱码问题,当然,丢到第三方日志分析工具glaylog下,也出现中文乱码。
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="UdpAppender" type="log4net.Appender.UdpAppender">
<param name="Encoding" value="utf-8" />
<remoteAddress value="192.168.1.43" />
<remotePort value="" />
<layout type="log4net.Layout.PatternLayout" value="%-5level %logger %message %username %date{yyyyMMdd HH:mm:ss fff} "/>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="ColoredConsoleAppender" />
<appender-ref ref="UdpAppender" />
</root> <logger name="*">
<level value="ALL" />
<appender-ref ref="UdpAppender" />
</logger>
</log4net>
</configuration>
上面的配置中,加上了<param name="Encoding" value="utf-8" /> 问题解决!
LogHelper 公共类
using log4net;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web; namespace Log4netDemo
{
public class LogHelper
{
//private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); /// <summary>
/// 每个函数都拥有自己的栈空间,为什么是3?
/// trace.GetFrame(0).GetMethod().Name = GetCurrentMethodFullName() 方法本身
/// trace.GetFrame(1).GetMethod().Name = log(),即调用GetCurrentMethodFullName()上一级
/// trace.GetFrame(2).GetMethod().Name = Info(),即 public static void Info(object message) ,log()的上一级
/// 所以,写死是3,3就是控制器了。
/// </summary>
/// <returns></returns>
private static string GetCurrentMethodFullName()
{
try
{
StackTrace trace = new StackTrace();
return trace.GetFrame().GetMethod().DeclaringType.ToString();
}
catch
{
return System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString();
}
} private static ILog log(string moduleName, string executor)
{
log4net.LogicalThreadContext.Properties["ModuleName"] = string.IsNullOrEmpty(moduleName)? "" : moduleName;
log4net.LogicalThreadContext.Properties["Executor"] = string.IsNullOrEmpty(executor) ? "" : executor;
return log4net.LogManager.GetLogger(GetCurrentMethodFullName());
} public static void Info(object message)
{
ILog nlog = log("", "");
nlog.Info(message);
} public static void Info(object message, string moduleName)
{
ILog nlog = log(moduleName, "");
nlog.Info(message);
} public static void Info(object message, string moduleName, string executor)
{
ILog nlog = log(moduleName, executor);
nlog.Info(message);
} public static void Error(object message)
{
ILog nlog = log("", "");
nlog.Error(message, null);
} public static void Error(Exception exception)
{
ILog nlog = log("", "");
nlog.Error("", exception);
} public static void Error(object message, string moduleName)
{
ILog nlog = log(moduleName, "");
nlog.Error(message, null);
} public static void Error(object message, string moduleName, string executor)
{
ILog nlog = log(moduleName, executor);
nlog.Error(message, null);
} public static void Error(object message, string moduleName, string executor, Exception exception)
{
ILog nlog = log(moduleName, executor);
nlog.Error(message, exception);
}
}
}