EntLib 6使用TraceListenerData

时间:2015-07-02 10:36:48

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

我想在SQL DB中使用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;
}

这是我在Unit测试中完成的Config字符串(我在其中启动了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"'.

感谢帮助我: - )

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案,我将None包含在调用者中(UnitTest)

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