企业库5.0-将自定义标记添加到TextFormatter [英] Enterprise Library 5.0 - Adding custom tokens to a TextFormatter

查看:155
本文介绍了企业库5.0-将自定义标记添加到TextFormatter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义异常,该异常具有与我们的应用程序有关的数据。我想确保在引发异常并记录到事件日志时记录这些数据。

I have created a custom Exception that has data pertaining to our application. I want to ensure this data gets logged when an exception is thrown and logged to the event log.

我尝试创建一个自定义TextFormatter,但该方法被调用但不确定如何访问当前异常,以便可以将我们的自定义信息添加到日志条目中。

I have tried creating a custom TextFormatter which is being called but am not sure how to access the current exception so I can add our custom information to the log entry.

有些事情我不了解,希望能为您添加自定义标记提供帮助(和数据)添加到Enterprise Library 5.0 TextFormatters。

There is something I am not understanding and would appreciate any help around adding custom tokens (and data) to Enterprise Library 5.0 TextFormatters.

谢谢,
... Marc

Thanks, ...Marc

推荐答案

您可以扩展 TextFormatter 。我认为这里的问题是 TextFormatter 格式化 LogEntry

You could extend TextFormatter. I think the issue there is that TextFormatter formats a LogEntry:

public override string Format(
    LogEntry log
)

因此,您需要将自定义数据放入LogEntry。 (我想这是您的问题要问的。)

So you need to get your custom data into the LogEntry. (Which I think is what your question is asking.)

另一种方法(我认为更简单)是使用 ExtendedProperties LogEntry 中的c $ c>。如果您使用异常处理块(EHAB)记录异常,则可以将所有自定义信息添加到 IDictionary Data 属性。在运行时,数据字典的内容将添加到 ExtendedProperties 中。然后可以使用格式化程序定义中的标记来记录特定属性。

An alternative (and I think simpler) approach is to use the ExtendedProperties of LogEntry. If you are using the Exception Handling Block (EHAB) to log your exception then you can add all of your custom information to the IDictionary Data property. At runtime, the contents of the Data dictionary are added to the ExtendedProperties. Specific properties can then be logged by using tokens in the formatter definition.

例如

public class MyException : Exception
{
    private string myCustomProperty;

    public string MyCustomProperty
    {
         get
         {
             return myCustomProperty;
         }
         set
         {
             myCustomProperty = value;
             Data["MyCustomProperty"] = value;
         }
    }
}

然后您可以处理该异常使用带有记录策略的EHAB:

Then you can handle the exception using EHAB with a logging policy:

ExceptionPolicy.HandleException(ex, "Logging Exception Handler");

在模板中添加以下内容:

In your template add something like the following:

MyCustomProperty:{keyvalue(MyCustomProperty)}

MyCustomProperty: {keyvalue(MyCustomProperty)}

这将记录您的自定义属性。

This will then log your custom property.

如果您没有使用EHAB,则可以在异常类中设置数据值,然后创建小型帮助程序类以将自定义属性添加到 ExtendedProperties

If you are not using EHAB then you could not set the Data value in your exception class and then create small helper class to add your custom property to the ExtendedProperties:

public void LogException(Exception ex)
{
    LogEntry logEntry = new LogEntry();
    //...
    logEntry.ExtendedProperties.Add("MyCustomProperty", ex.MyCustomProperty);

    Logger.Write(logEntry);
}

如果记录自定义异常属性对您来说是一个普遍问题,则可以添加将所有自定义属性添加到数据字典中,然后在助手方法中将所有数据键/值对复制到ExtendedProperties。

If logging of custom exception properties is a general problem for you then you could add all of your custom properties to the Data dictionary and then in the helper method copy all of the Data key/value pairs to the ExtendedProperties.

这篇关于企业库5.0-将自定义标记添加到TextFormatter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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