【问题标题】:Enterprise Library 5.0 Logging Application Block: Log Custome Message using a DatabaseTraceListenerEnterprise Library 5.0 日志记录应用程序块:使用数据库跟踪侦听器记录自定义消息
【发布时间】:2012-07-10 06:24:44
【问题描述】:

我正在使用 Enterprise Library 5.0,我的任务是将信息记录到数据库中。为此,我使用了 Enterprise Library Logging Application Block 和 Database Trace Listener。

现在,几天后我发现我需要更多信息才能登录 Logging.LOG 表。它们是:User_ID、Session_ID 等。

根据业务需求,我不能使用 LogEntry 类的 ExtendedProperty 属性并将此信息存储在 XML 格式的日志表的 FormattedMessage 列中。我希望在日志数据库的日志表中具有名称为“User_ID”和“Session_ID”的正确列。

问题是:如何在日志表中推送自定义信息?

为此,我发现了一个名为“CustomTraceListener”的东西,我们需要覆盖两个方法,即 Write(string Message) & WriteLog(string Message)

  • 如何使用此 CustomeTraceListener 将客户消息推送到日志表?

  • Write 方法采用字符串类型的单个参数,意味着我会得到一个连接字符串,我需要将其分解为不同的信息并使用我自己的代码推送到数据库中吗?

谁能给我发一个CustomeTraceListener的实现示例到-->数据库

请帮帮我。

谢谢,

苏拉杰

【问题讨论】:

    标签: enterprise-library logging-application-block


    【解决方案1】:

    按照此创建自定义跟踪侦听器:http://msdn.microsoft.com/en-us/library/ff647545.aspx

    然后,您可以使用 Fluent 配置执行类似的操作(代码不起作用,但给您一个想法):

        var builder = new ConfigurationSourceBuilder();
        var serviceConfig = new NameValueCollection();
        serviceConfig.Add("Key", "data");
        builder.ConfigureLogging()
            .LogToCategoryNamed("General")
            .WithOptions.SetAsDefaultCategory()
            .SendTo.Custom<ServiceTraceListener>("ServiceTraceListener", serviceConfig)
            .FormatWith(new FormatterBuilder()
            .TextFormatterNamed("Text Formatter")
            .UsingTemplate("Timestamp: {timestamp}...{newline})}"));
    
        var configSource = new DictionaryConfigurationSource();
        configSource.Add(LoggingSettings.SectionName, builder.Get(LoggingSettings.SectionName));
        var cont = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
        return cont.GetInstance<LogWriter>();
    

    (代码 ± 来自:Enterprise Library 5.0 Logging using CustomTraceListener and ConfigurationSourceBuilder

    以下是您可以在 .UsingTemplate 方法中使用的数据概览: http://msdn.microsoft.com/en-us/library/microsoft.practices.enterpriselibrary.logging.configuration.textformatterdata.template(v=PandP.50).aspx

    然后,此模板将在您的 CustomTraceListener TraceData 覆盖方法中使用(如果您按照我在此处链接的 MSDN 中的演练进行操作)。

    另见:http://msdn.microsoft.com/en-us/library/ff664363(v=PandP.50).aspx 额外格式:http://msdn.microsoft.com/en-us/library/ff664562(v=PandP.50).aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-21
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 1970-01-01
      • 2016-05-12
      • 1970-01-01
      相关资源
      最近更新 更多