Entlib和互操作:它的工作原理,以及哪里的配置文件去?
-
21-08-2019 - |
题
我试图使用EntLib 3.1 .NET代码内,用于被注册为COM互操作的DLL。我应该把配置文件?
另外,有到该DLL的代码中指定它应该从entlib配置的方法吗?由于我的DLL从COM调用我并不总是知道什么是exe文件将调用它。
我创建了一个简单的应用程序,它使用entlib记录,具有两个类:“CallingApp”和“MyComThing”。当我打电话MyComThing的方法从CallingApp它记录使用CallingApp的配置文件中的配置。当我打电话MyComThing的方法从一个VBS脚本,即通过COM,我得到一个错误“的日志记录配置节不能配置源中找到”。我的COMThing.dll.config文件是在同一文件夹中注册的COMThing.dll,即在bin \调试\文件夹。
谢谢!
解决方案
答案是,企业库默认使用的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.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:\ Windows \ System32下\ WScript.exe的(或相似的),它变得从您的DLL的配置文件的配置
我根据本上的信息此处在“使用几个ConfigurationSources”。
请注意的是,记录器类包括大量的好的辅助方法具有不同的参数。由于我们使用的日志写的类,我们没有得到这个魔法。个人而言,我将创建另一个类我的图书馆内进行同样的工作的基础上,记录仪。
在引用的文章示出了应用于数据库和异常应用程序块相同的原理。据推测相同的模型可以适用于大多数/所有这些。
其他提示
检查,我曾遇到的有关问题。也许有一定的帮助的。