如何登录异常的文件? [英] How to Log Exception in a file?

查看:100
本文介绍了如何登录异常的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够做记录在每一个catch块。 。事情是这样的。



 赶上(例外的例外)
{
Logger.Write(例外);
}



,然后在配置中的设置将拿起消息和堆栈跟踪财产等利用客户的倾听者。



我想用企业库日志应用程序块。我敢肯定,一定是有人已经这样做了。


解决方案

其他人张贴在获取记录应用程序块一些很好的链接(LAB )工作,所以我不会重复,这里



在格式化你的例外条款你有3个选择,我能想到的:




  1. 使用默认的 Exception.ToString() 实现(这不是坏)

  2. 编写自定义格式化程序集成到LAB

  3. 编写执行格式化并通过串入Write方法的辅助功能。



< 。p>如果选项1不能满足您的需求,那么我会建议使用选项3将(因为选项2是矫枉过正)



一个简单的例子是这样的:

 赶上(例外的例外)
{
Logger.Write(LogHelper.CreateExceptionString(例外));
}

...

公共静态字符串CreateExceptionString(例外五)
{
StringBuilder的SB =新的StringBuilder();
CreateExceptionString(SB,E的String.Empty);

返回sb.ToString();
}

私有静态无效CreateExceptionString(StringBuilder的某人,例外五,串缩进)
{
如果(缩进== NULL)
{
缩进=的String.Empty;
}
,否则如果(indent.Length大于0)
{
sb.AppendFormat({0}内,缩进);
}

sb.AppendFormat(异常发现:\\\
{0}类型:{1},缩进,e.GetType()全名);
sb.AppendFormat(\\\
{0}消息:{1},缩进,e.Message);
sb.AppendFormat(\\\
{0}资料来源:{1},缩进,e.Source);
sb.AppendFormat(\\\
{0}堆栈跟踪:{1},缩进,e.StackTrace);

如果(e.InnerException!= NULL)
{
sb.Append(\\\
);
CreateExceptionString(SB,e.InnerException,缩进+);
}
}


I want to be able to do logging in every catch block. Something like this.

catch (Exception exception)
{
  Logger.Write(exception);
}

and then the settings in the configuration will pick up the Message and StackTrace property etc using customer listener.

I would like to use Enterprise Library Logging Application Block. I'm sure someone must have done this already.

解决方案

Others have posted some good links on getting the Logging Application Block (LAB) working so I won't duplicate that here.

In terms of formatting your exception you have 3 choices that I can think of:

  1. Use the default Exception.ToString() implementation (it's not bad)
  2. Write a Custom Formatter that integrates into the LAB.
  3. Write a helper function that performs the formatting and passes the string into the Write method.

If option 1 doesn't meet your needs then I would recommend going with option 3 (since option 2 is overkill).

A simple example would be something like:

    catch (Exception exception)
    {
        Logger.Write(LogHelper.CreateExceptionString(exception));
    }

    ...

    public static string CreateExceptionString(Exception e)
    {
        StringBuilder sb = new StringBuilder();
        CreateExceptionString(sb, e, String.Empty);

        return sb.ToString();
    }

    private static void CreateExceptionString(StringBuilder sb, Exception e, string indent)
    {
        if (indent == null)
        {
            indent = String.Empty;
        }
        else if (indent.Length > 0)
        {
            sb.AppendFormat("{0}Inner ", indent);
        }

        sb.AppendFormat("Exception Found:\n{0}Type: {1}", indent, e.GetType().FullName);
        sb.AppendFormat("\n{0}Message: {1}", indent, e.Message);
        sb.AppendFormat("\n{0}Source: {1}", indent, e.Source);
        sb.AppendFormat("\n{0}Stacktrace: {1}", indent, e.StackTrace);

        if (e.InnerException != null)
        {
            sb.Append("\n");
            CreateExceptionString(sb, e.InnerException, indent + "  ");
        }
    }

这篇关于如何登录异常的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆