【问题标题】:EntLib 6 Logging Block on SQL issue with TraceListenerDataEntLib 6 与 TraceListenerData 的 SQL 问题有关的日志记录块
【发布时间】:2015-09-19 20:19:35
【问题描述】:

我想用 Enterprise Library 6 在 SQL DB 中记录我的应用程序。

我启动了 SQL 脚本来创建 SQL 事物,我决定将 Logger 放入类库中。

这是我为制作 POC 所做的代码:

便携类内容:

public static void LogToDatabase()
{
    try
    {
        //Bootstrapping logging
        DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());
        IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
        LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
        Logger.SetLogWriter(logWriterFactory.Create());

        if (Logger.IsLoggingEnabled())
        {

            LogEntry log = GetLogEntry();
            log.Categories.Add("General");  // name of Categorie in EntLib Config editor
            log.Priority = 5;
            log.Severity = System.Diagnostics.TraceEventType.Information;
            log.Message = "Hello dude, from Logger";
            Logger.Write(log);

        }

    }
    catch (Exception ex)
    {
    }
}

private static LogEntry GetLogEntry()
{
    LogEntry log = new LogEntry();
    log.TimeStamp = DateTime.Now;
    log.Title = "TANOLIS";
    log.Priority = 5;                       // default priority
    log.Severity = System.Diagnostics.TraceEventType.Verbose;  // default severity
    log.MachineName = "MachineName";//HttpContext.Current.Server.MachineName;
    log.ProcessId = System.Diagnostics.Process.GetCurrentProcess().Id.ToString();
    return log;
}

这是我在单元测试中完成的配置字符串(我在其中启动 LogToDatabase()):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
  <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
    <listeners>
      <add name="Database Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        databaseInstanceName="DB_Belval" writeLogStoredProcName="WriteLog"
        addCategoryStoredProcName="AddCategory" formatter="DB Command Formatter" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
        name="DB Command Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Database Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Database Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
  <dataConfiguration defaultDatabase="MY-DB" />
  <connectionStrings>
    <add name="DB_Belval" connectionString="Data Source=server;Initial Catalog=catalog;Persist Security Info=True;User=username;Password=pwd"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

错误出现在这一行:

LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);

并且是

Invalid TraceListenerData type in configuration 'listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"'.

感谢帮助我:-)

【问题讨论】:

    标签: c# sql-server logging enterprise-library tracelistener


    【解决方案1】:

    我终于找到了解决方案,我将using Microsoft.Practices.EnterpriseLibrary.Logging.Database; 包含在调用者(UnitTest)中

    问题已解决,但仍然无法正常工作...所以警告,不要使用整个代码 :-)

    【讨论】:

    • 可能没有部署 Microsoft.Practices.EnterpriseLibrary.Logging.Database 程序集,因为它是软依赖项(在配置中)。如果这是一个单元测试,您可以使用DeploymentItemAttribute 强制部署程序集。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 2011-09-24
    • 1970-01-01
    相关资源
    最近更新 更多