答案是企业库默认使用 exe 的配置文件。如果您正在生成一个 dll,包括 COM,那么出于充分的理由,您可能不想依赖调用可执行文件。对此的一种解决方案(可能还有其他解决方案)是自己创建企业库对象而不是使用默认对象,并告诉他们从哪里获取配置。这并不像看起来那么可怕,并且不需要重新编译 entlib 或类似的东西。
而不是简单地使用 Logger.Write() 我做了以下事情: a) 创建日志写入器,使用 dll 的配置文件:
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>
在 Build 下的项目属性中,检查“Register for COM interop”。构建项目,然后创建以下 .vbs 文件:
Set obj = CreateObject("COMThing.MyComThing")
obj.ProcessMessage("called from com by vbs")
如果你双击这个 vbs 文件,它应该会显示一个带有文本“由 vbs 从 com 调用”的消息框,并在应用程序事件日志中写入一个条目。这表明,虽然执行过程是 C:\WINDOWS\System32\WScript.exe(或类似的),但它正在从您的 dll 的配置文件中获取配置。
我基于“使用多个配置源”下的信息。
请注意,Logger 类包含许多具有不同参数的好帮手方法。由于我们使用的是 LogWriter 类,因此我们无法获得这种魔力。就我个人而言,我将在我的库中创建另一个类来执行相同的工作,基于 Logger。
引用的文章显示了应用于数据库和异常应用程序块的相同原理。大概相同的模型可以应用于大多数/全部。