如何配置enterprise Library 6.0的数据库日志
本文关键字:数据库 日志 Library enterprise 何配置 配置 | 更新日期: 2023-09-27 18:05:16
在我的App.config中我有第二件事:
<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}
Guid: {property(HandlingInstanceId)}{newline}
Timestamp: {timestamp}{newline}
Message: {message}{newline}
Exception type: {property(ExceptionType)}{newline}
Category: {category}{newline}
Severity: {severity}{newline}
Machine: {localMachine}{newline}
App Domain: {localAppDomain}{newline}
ProcessId: {localProcessId}{newline}
Process Name: {localProcessName}{newline}
Stack trace: {property(StackTrace)}{newline}
File: {property(FileName)}{newline}
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 & Warnings">
<listeners>
<add name="Database Trace Listener" />
<add name="Rolling Flat File Trace Listener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
我还在初始化中添加了这一部分:
DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());
LogWriterFactory logWriterFactory = new LogWriterFactory();
And, Exception:
private static void LogExceptionDetails(Exception ex)
{
LogEntry logEntry = new LogEntry
{
Message = ex.Message
};
Logger.Write(logEntry);
}
到目前为止我不能得到的是,我想如何检测,在哪个表异常将被记录。我的意思是,为了让企业看到核心,我需要添加:
<connectionStrings>
<add name="Core" connectionString="blablabla" />
</connectionStrings>
但这仍然没有解释如何选择先知表和格式化它(我知道的唯一方法是创建过程,并把它在writeLogStoredProcName=但我在4.1中使用了这个,我从未触及6.0之前)。
此外,问题是我所有的确认都存储在另一个数据库中,这就是为什么我想从App.config中摆脱connectionString的原因。
总结一下,我的问题是
是否有一种方法在App.config(和过程)之外设置数据库,表和表列,用于企业库日志到数据库?也许它是在某个地方DatabaseFactory?
p。S本地登录到rolling.log文件工作正常。
EDIT:看起来我没有选择,我需要使用过程来编写所需的表。仍然试图找到一种方法来设置连接字符串外的App.config
您发现了开箱即用的FormattedDatabaseTraceListener
使用存储过程写入数据库。您可以配置存储过程的名称,但存储过程包含特定于数据库的逻辑。只要符合存储过程接口,就可以修改或创建新的存储过程来执行任何需要的操作:
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
另一种选择是创建一个自定义跟踪侦听器来做您想做的事情。
关于在外部配置文件中放置connectionStrings,你可以使用内置的。net configSource属性:
<connectionStrings configSource="connections.config" />
或者企业库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
以编程方式加载配置。
参见我对EnterpriseLibrary数据访问应用程序块重定向节无法找到默认数据库的回答查看详细信息。
如果你想混合和匹配声明性XML日志配置和运行时编程数据库配置,那么你可以使用类似的东西:
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;";
}
}