Entlibと相互運用:それは動作しませんし、設定ファイルはどこに行くのですか?
-
21-08-2019 - |
質問
私は、COM相互運用のために登録されているDLLの.NETコード内EntLib 3.1を使用しようとしています。どこで設定ファイルを置けばいいの?
また、それからentlibの設定を取得する必要がありdllファイルのコード内で指定する方法はありますか?私のDLLがCOMから呼び出されますので、私はいつもexeファイルがそれを呼び出すことになるかわからない。
「CallingApp」と「MyComThing」私は2つのクラスで、entlibロギングを使用するシンプルなアプリを作成しました。私はCallingAppからMyComThingのメソッドを呼び出すと、それはCallingAppのconfigファイル内の設定を使用して記録します。私はつまり、COMを通じて、VBSスクリプトからMyComThingのメソッドを呼び出すと、私は、「ロギングの構成セクションが設定ソースで見つけることができません」エラーが発生します。マイCOMThing.dll.configファイルには、すなわち、ビン\デバッグ\フォルダに、登録COMThing.dllと同じフォルダにあります。
ありがとう!
解決
答えは、エンタープライズライブラリは、デフォルトでexeファイルのconfigファイルを使用していることです。あなたはCOMを含むDLLを製造している場合には、正当な理由のためにあなたは、呼び出し元の実行ファイルに依存したくない場合があります。 (他の人があるかもしれない)これに対する一つの解決策は、エンタープライズライブラリではなく、デフォルトのものを使用しての自分でオブジェクトを作成し、どこからコンフィギュレーションを得るためにそれらを伝えることです。これは、それはそうとentlibまたはそのような何かを再コンパイルする必要がないほど怖いではありません。
の代わりに、単純に私は次のようでしたLogger.Write()を使用しての: a)は、DLLのconfigファイルを使用して、ログライターを作成します。
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からのいわゆる」テキストとメッセージボックスを表示し、あなたのアプリケーションイベントログにエントリを書き込む必要があるファイルをダブルクリックした場合。 \ WINDOWS \ System32に\ WScript.exeという(または類似の)、それはあなたのDLLのconfigファイルから設定を取得しています。
:これは、実行中のプロセスがCである一方であることを示しています。私はここの情報にこれをベースとする 「いくつかのConfigurationSourcesを使用して」下。
Loggerクラスは、異なる引数を持つ素敵なヘルパーメソッドの多くが含まれていることに注意してください。私たちは、ログ・ライタークラスを使用しているので、我々はこの魔法を得ることはありません。個人的に私はロガーに基づいて、同じジョブを実行するために、私のライブラリ内の別のクラスを作成することがあります。
の論文は、データベースとアプリケーションの例外ブロックに適用されたものと同じ原理を示します。おそらく同じモデルは、それらのほとんど/全てに適用することができます。
他のヒント
私が直面していた関連の問題を確認してください。たぶん、そのいくつかの助けのます。