答案是,Enterprise Library默认使用exe s配置文件。如果您正在生成dll,包括COM,则由于某些充分的理由,您可能不希望依赖于调用可执行文件。解决此问题的一个解决方案(可能还有其他解决方案)是自己创建Enterprise Library对象,而不是使用默认的对象,并告诉它们从哪里获取配置。这并不像看起来那么可怕,也不需要重新编译entlib或其他任何东西。
Instead of simply using Logger.Write() I did the following:
a) Create the log writer, using the dll s config file:
string dllConfigFilename = Assembly.GetExecutingAssembly().Location + ".config";
FileConfigurationSource exceptionsSource = new FileConfigurationSource(dllConfigFilename);
LogWriterFactory writerFactory = new LogWriterFactory(exceptionsSource);
logWriter = writerFactory.Create();
B)然后在您的代码中使用此日志编写器:
LogEntry log = new LogEntry();
log.Message = message;
log.Categories = new string[] { "General" };
logWriter.Write(log);
这是我创建的一个样本对象的完整代码。引用包括Microsoft.Practices.EnterpriseLibrary.Common,Microsoft.Practices.EnterpriseLibrary.Logging,Microsoft.Practices.ObjectBuilder,System,System.Data,System.Windows.Forms和System.Xml:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
namespace COMThing
{
[ComVisible(true)]
public class MyComThing : MyComInterface
{
LogWriter logWriter;
public MyComThing()
{
string dllConfigFilename = Assembly.GetExecutingAssembly().Location + ".config";
FileConfigurationSource exceptionsSource = new FileConfigurationSource(dllConfigFilename);
LogWriterFactory writerFactory = new LogWriterFactory(exceptionsSource);
logWriter = writerFactory.Create();
}
public bool ProcessMessage(string message)
{
LogEntry log = new LogEntry();
log.Message = message;
log.Categories = new string[] { "General" };
logWriter.Write(log);
MessageBox.Show(message);
return true;
}
}
}
该项目包括一个COMThing.dll.config文件,我设置了“始终复制”到“输出目录”。这是一个简单的配置,将日志信息写入应用程序事件日志。配置文件的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</configSections>
<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
<listeners>
<add source="COMThing Logger" formatter="Text Formatter" log="Application"
machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="Formatted EventLog TraceListener" />
</listeners>
<formatters>
<add template="Timestamp: {timestamp}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Title:{title}
Machine: {machine}
Application Domain: {appDomain}
Process Id: {processId}
Process Name: {processName}
Win32 Thread Id: {win32ThreadId}
Thread Name: {threadName}
Extended Properties: {dictionary({key} - {value}
)}"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Formatted EventLog TraceListener" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events">
<listeners>
<add name="Formatted EventLog TraceListener" />
</listeners>
</allEvents>
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors & Warnings">
<listeners>
<add name="Formatted EventLog TraceListener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
</configuration>
在项目属性下的构建中,勾选“注册为COM互操作”。构建项目,然后创建以下.vbs文件:
Set obj = CreateObject("COMThing.MyComThing")
obj.ProcessMessage("called from com by vbs")
如果您双击此vbs文件,则应显示一个包含文本“从vbs调用的com”消息框,并向您的应用程序事件日志中写入条目。这说明尽管执行进程是C:WINDOWSSystem32WScript.exe(或类似的进程),但它正在从您的dll的配置文件中获取配置。
我基于此信息进行了编写 这里 的多个配置来源。
请注意,Logger类包含许多带有不同参数的有用辅助方法。由于我们正在使用LogWriter类,我们不会得到这种神奇效果。个人而言,我将在我的库中创建另一个类来执行相同的工作,基于Logger。
所引用的文章展示了相同的原则应用于数据库和异常应用程序块。可以假设相同的模型可以应用于大多数/所有应用程序块。