TraceListenerData的SQL问题上的EntLib 6日志记录块

本文关键字:日志 记录块 EntLib SQL 问题 TraceListenerData | 更新日期: 2023-09-27 18:00:43

我想在SQL数据库中使用Enterprise Library 6记录我的应用程序。

我启动了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"'.

感谢您的帮助:-)

TraceListenerData的SQL问题上的EntLib 6日志记录块

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

问题解决了,但仍然不起作用。。。所以警告,不要接受整个代码:-)