小编Ste*_*ker的帖子

使用log4net为.NET项目记录ClassName和MethodName

我一直在寻找一种方法来记录类名和方法名作为我的日志记录基础结构的一部分.显然,我想在运行时使用简单快捷.我已经做了大量关于记录类名和方法名的阅读,但我遇到了2个主题.

  1. log4net使用内部抛出异常来生成堆栈帧,如果通常对所有日志记录使用它,则会变得昂贵.
  2. 混乱.那里有很多文献.我已经尝试了一堆它并没有得到一些有用的东西.

如果你幽默我一秒钟,我想重置.

我在我的项目中创建了这样一个类

public static class Log {
    private static Dictionary<Type, ILog> _loggers = new Dictionary<Type, ILog>();
    private static bool _logInitialized = false;
    private static object _lock = new object();

    public static string SerializeException(Exception e) {
        return SerializeException(e, string.Empty);
    }

    private static string SerializeException(Exception e, string exceptionMessage) {
        if (e == null) return string.Empty;

        exceptionMessage = string.Format(
            "{0}{1}{2}\n{3}",
            exceptionMessage,
            (exceptionMessage == string.Empty) ? string.Empty : "\n\n",
            e.Message,
            e.StackTrace);

        if (e.InnerException != null)
            exceptionMessage = SerializeException(e.InnerException, exceptionMessage);

        return exceptionMessage; …
Run Code Online (Sandbox Code Playgroud)

c# logging log4net

9
推荐指数
2
解决办法
2万
查看次数

标签 统计

c# ×1

log4net ×1

logging ×1