zoukankan      html  css  js  c++  java
  • Log4net 乱码问题解决

    在将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="12201" />
          <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 公共类

     1 using log4net;
     2 using System;
     3 using System.Collections.Generic;
     4 using System.Diagnostics;
     5 using System.Linq;
     6 using System.Web;
     7 
     8 namespace Log4netDemo
     9 {
    10     public class LogHelper
    11     {
    12         //private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    13 
    14         /// <summary>
    15         /// 每个函数都拥有自己的栈空间,为什么是3?
    16         /// trace.GetFrame(0).GetMethod().Name = GetCurrentMethodFullName() 方法本身
    17         /// trace.GetFrame(1).GetMethod().Name = log(),即调用GetCurrentMethodFullName()上一级
    18         /// trace.GetFrame(2).GetMethod().Name = Info(),即 public static void Info(object message) ,log()的上一级
    19         /// 所以,写死是3,3就是控制器了。
    20         /// </summary>
    21         /// <returns></returns>
    22         private static string GetCurrentMethodFullName()
    23         {
    24             try
    25             {
    26                 StackTrace trace = new StackTrace();
    27                 return trace.GetFrame(3).GetMethod().DeclaringType.ToString();
    28             }
    29             catch
    30             {
    31                 return System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString();
    32             }
    33         }
    34 
    35         private static ILog log(string moduleName, string executor)
    36         {
    37             log4net.LogicalThreadContext.Properties["ModuleName"] = string.IsNullOrEmpty(moduleName)? "" : moduleName;
    38             log4net.LogicalThreadContext.Properties["Executor"] = string.IsNullOrEmpty(executor) ? "" : executor;
    39             return log4net.LogManager.GetLogger(GetCurrentMethodFullName());
    40         }
    41 
    42         public static void Info(object message)
    43         {
    44             ILog nlog = log("", "");
    45             nlog.Info(message);
    46         }
    47 
    48         public static void Info(object message, string moduleName)
    49         {
    50             ILog nlog = log(moduleName, "");
    51             nlog.Info(message);
    52         }
    53 
    54         public static void Info(object message, string moduleName, string executor)
    55         {
    56             ILog nlog = log(moduleName, executor);
    57             nlog.Info(message);
    58         }
    59 
    60         public static void Error(object message)
    61         {
    62             ILog nlog = log("", "");
    63             nlog.Error(message, null);
    64         }
    65 
    66         public static void Error(Exception exception)
    67         {
    68             ILog nlog = log("", "");
    69             nlog.Error("", exception);
    70         }
    71 
    72         public static void Error(object message, string moduleName)
    73         {
    74             ILog nlog = log(moduleName, "");
    75             nlog.Error(message, null);
    76         }
    77 
    78         public static void Error(object message, string moduleName, string executor)
    79         {
    80             ILog nlog = log(moduleName, executor);
    81             nlog.Error(message, null);
    82         }
    83 
    84         public static void Error(object message, string moduleName, string executor, Exception exception)
    85         {
    86             ILog nlog = log(moduleName, executor);
    87             nlog.Error(message, exception);
    88         }
    89     }
    90 }
    View Code
  • 相关阅读:
    SDOI2011古代朱文
    LIS 堆优化
    ZR2019 广州 游记
    LG2709 小B的询问
    [SCOI2009] 生日礼物
    [SDOI2008]沙拉公主的困惑
    [LG3396]哈希冲突
    ZROI2018.8.2 菜鸡互啄杯组队 ACM 赛
    ZROI 菜鸡互啄杯 III
    [LG4016] 负载平衡问题
  • 原文地址:https://www.cnblogs.com/jys509/p/4702275.html
Copyright © 2011-2022 走看看