如何配置Enterpise库6.0日志数据库? [英] How to configure Enterpise Library 6.0 logging to database?

查看:461
本文介绍了如何配置Enterpise库6.0日志数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的App.config我有第二件事情:

In my App.config I have a second things:

  <configSections>
    <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" />
    <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" />
  </configSections>
<dataConfiguration defaultDatabase="Core" />
  <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="Core" 
           writeLogStoredProcName="WriteLog"
           addCategoryStoredProcName="AddCategory" />
      <add name="Rolling Flat File Trace Listener" 
           type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
           listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
           fileName="rolling.log" 
           formatter="Text Formatter" 
           rollInterval="Hour" 
           rollSizeKB="10000" 
           traceOutputOptions="DateTime, Callstack" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
           template="Application: {property(ApplicationName)}{newline}&#xA;Guid: {property(HandlingInstanceId)}{newline}&#xA;Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Exception type: {property(ExceptionType)}{newline}&#xA;Category: {category}{newline}&#xA;Severity: {severity}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Stack trace: {property(StackTrace)}{newline}&#xA;File: {property(FileName)}{newline}&#xA;Line: {property(LineNumber)}{newline}" 
           name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Database Trace Listener" />
          <add name="Rolling Flat File 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" />
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>



我还添加了这部分进入初始化:

I also added this part into initialisation:

DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());
LogWriterFactory logWriterFactory = new LogWriterFactory();

和,异常写:

private static void LogExceptionDetails(Exception ex)
    {
        LogEntry logEntry = new LogEntry
        {
            Message = ex.Message
        };
        Logger.Write(logEntry);
    }



我不能得到到目前为止,我怎么想检测的事情,在这表异常将被记录。
我的意思是,使企业看到核心,我需要添加:

The thing I cant get so far is, how I suppose to detect, in which table exception will be logged. I mean, to make Enterprise see Core, I need to add:

<connectionStrings>
   <add name="Core" connectionString="blablabla" />
</connectionStrings>



但是,这仍然合乎理解释如何选择propher表,并对其进行格式化(我知道的唯一途径是建立程序,并把它在writeLogStoredProcName =但我在4.1用这个,我从来没有碰过6.0之前)。

But this still doest explain how to select propher table and format it (the only way I know is creating procedure and putting it in writeLogStoredProcName= but I used this in 4.1, I never touched 6.0 before).

此外,问题是,我所有的confirugations存储在另一个数据库,这就是为什么我想从App.config中摆脱的connectionString)的原因。

Also, the problem is that all my confirugations are stored in another database, and this is the reason why I want to get rid of connectionString from App.config).

因此,要总结,我的问题是

有没有办法设置数据库,表和表列的App.config(和程序)企业图书馆外记录到数据库?也许它是在什么地方DatabaseFactory做?

Is there a way to set database, table and table columns outside the App.config (and procedure) for Enterprise Library Logging to database? Maybe it is done somewhere in DatabaseFactory?

PS本地登录到rolling.log文件工作正常。

P.S Local logging into rolling.log file works fine.

< STRONG>编辑:貌似我没有最佳的选择,我需要使用过程中的所需表格记录。仍在试图找到一种方法来设置的App.config

Looks like I have no choise and I need to use procedure to write in needed table. Still trying to find a way to set connectionstring outside the App.config

推荐答案

之外的ConnectionString当你发现了开箱 FormattedDatabaseTraceListener 写入到使用存储过程的数据库。您可以配置存储过程的名称,但存储过程包含数据库特定的逻辑。您可以修改或创建一个新的存储过程做你想做的,只要它符合存储过程的接口,无论:

As you found out the out of the box FormattedDatabaseTraceListener writes to the database using a stored procedure. You can configure the name of the stored procedure but a stored procedure contains the database specific logic. You can modify or create a new stored procedure to do whatever you want as long as it conforms to the stored procedure interfaces:

CREATE PROCEDURE [dbo].[WriteLog]
(
    @EventID int, 
    @Priority int, 
    @Severity nvarchar(32), 
    @Title nvarchar(256), 
    @Timestamp datetime,
    @MachineName nvarchar(32), 
    @AppDomainName nvarchar(512),
    @ProcessID nvarchar(256),
    @ProcessName nvarchar(512),
    @ThreadName nvarchar(512),
    @Win32ThreadId nvarchar(128),
    @Message nvarchar(1500),
    @FormattedMessage ntext,
    @LogId int OUTPUT
)

CREATE PROCEDURE [dbo].[AddCategory]
    @CategoryName nvarchar(64),
    @LogID int

另一种方法是创建一个自定义跟踪监听器做的正是你想要的。

The other alternative is to create a custom trace listener to do exactly what you want.

在放置在是connectionStrings外部配置文件,您可以使用方面内置.NET configSource属性:

In terms of placing the connectionStrings in an external configuration file you can use the built-in .NET configSource attribute:

<connectionStrings configSource="connections.config" />

或者企业库redirectSections:

Or the Enterprise Library redirectSections:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
  <enterpriseLibrary.ConfigurationSource selectedSource="System Configuration Source">
    <sources>
      <add name="System Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      <add name="File-based Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        filePath="data.config" />
    </sources>
    <redirectSections>
      <add sourceName="File-based Configuration Source" name="dataConfiguration" />
      <add sourceName="File-based Configuration Source" name="connectionStrings" />
    </redirectSections>
  </enterpriseLibrary.ConfigurationSource>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

如果你想成为完全的app.config独立,那么你可以将所有的配置在外部配置文件,然后以编程方式使用 FileConfigurationSource

If you want to be completely app.config "independent" then you can place all configuration in an external configuration file and then load that configuration programmatically using a FileConfigurationSource.

请参阅我的回答的 EnterpriseLibrary数据访问应用程序块重定向科找不到默认的数据库
的更多细节。

See my answer to EnterpriseLibrary Data Access Application Block redirect Section can't find default database for more details.

如果你想混用和运行程序的数据库配置相匹配声明XML日志配置,那么你可以使用类似的东西它做于:

If you want to mix and match declarative XML logging configuration with runtime programmatic database configuration then you can do it using something similar to:

class Program
{
    static void Main(string[] args)
    {
        // Configure databases from some external source but ensure that 
        // database name matches XML config
        DatabaseFactory.SetDatabases(
            () => CreateDatabaseFromExternalSource(null),
            name => CreateDatabaseFromExternalSource(name));

        // Configure logging from XML config 
        LogWriterFactory factory = new LogWriterFactory();
        Logger.SetLogWriter(factory.Create());

        Logger.Write("Test", "General");
    }

    private static Database CreateDatabaseFromExternalSource(string name)
    {
        // SqlDatabase assumes SQL Server database
        return new SqlDatabase(GetConnectionString(name));
    }

    private static string GetConnectionString(string name)
    {
        // do whatever you need to get the database connection
        return @"Server=.\SQLEXPRESS;Database=myDataBase;User Id=myUsername;Password=myPassword;";
    }
}

这篇关于如何配置Enterpise库6.0日志数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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