Microsoft Enterprise Library 4.1でのロギング

この記事では、Microsoft Enterprise Library 4.1のロギングユニットの使用方法について説明します。 私は、 Patterns&Practices Roadmap Kievにアクセスして、それを理解することにしました。

エンタープライズライブラリをインストールする


ここから Enterprise Library 4.1 ダウンロードしてください 。 ロギングユニットに加えて、他の多くのコンポーネントがインストールされていることに注意してください。


Visual Studioで新しいプロジェクトを作成する


Viual Studioを開き、新しいコンソールアプリケーションを作成して、 HelloWorldEntLibLoggingなどの名前を付けます。

次に、ライブラリへの参照を追加する必要があります。

画像

ロギング構成


ロギングユニットを含むほとんどのEntLib機能が設定されています。 構成ファイルをプロジェクトに追加し、ライブラリとともにインストールされたVisual Studioのアドオンを使用して情報を追加する必要があります。

ソリューションエクスプローラーを使用して、新しいアプリケーション構成ファイルを追加し、デフォルトのファイル名app.configのままにします。

画像

次に、app.configを右クリックして、[エンタープライズライブラリ構成の編集]を選択します。

画像

開いたエディターで、「新規」を選択します| ロギングアプリケーションブロック:

画像

新しいロギングアプリケーションブロックがエディターツリーに表示されます。

画像

エディターを閉じ、app.configを開きます。

1: <? xml version ="1.0" encoding ="utf-8" ? >
2: < configuration >
3: < configSections >
4: < section name ="loggingConfiguration" type ="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
5: < section name ="dataConfiguration" type ="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
6: </ configSections >
7: < loggingConfiguration name ="Logging Application Block" tracingEnabled ="true"
8: defaultCategory ="General" logWarningsWhenNoCategoriesMatch ="true" >
9: < listeners >
10: < add source ="Enterprise Library Logging" formatter ="Text Formatter"
11: log ="Application" machineName ="" listenerDataType ="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
12: traceOutputOptions ="None" filter ="All" type ="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
13: name ="Formatted EventLog TraceListener" />
14: </ listeners >
15: < formatters >
16: < 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} )}"
17: type ="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
18: name ="Text Formatter" />
19: </ formatters >
20: < categorySources >
21: < add switchValue ="All" name ="General" >
22: < listeners >
23: < add name ="Formatted EventLog TraceListener" />
24: </ listeners >
25: </ add >
26: </ categorySources >
27: < specialSources >
28: < allEvents switchValue ="All" name ="All Events" />
29: < notProcessed switchValue ="All" name ="Unprocessed Category" />
30: < errors switchValue ="All" name ="Logging Errors & Warnings" >
31: < listeners >
32: < add name ="Formatted EventLog TraceListener" />
33: </ listeners >
34: </ errors >
35: </ specialSources >
36: </ loggingConfiguration >
37: </ configuration >


* This source code was highlighted with Source Code Highlighter .

現在、ファイルにはさまざまな設定がありますが、現時点ではそれらの設定は重要ではありません。

ロギングコードをアプリケーションに追加します


名前空間を接続します。

using Microsoft.Practices.EnterpriseLibrary.Logging;

* This source code was highlighted with Source Code Highlighter .

次に、次のコードをProgram.csに追加します。

class Program
{
static void Main( string [] args)
{
LogEntry entry = new LogEntry()
{
Message = "Hello Ent. Lib. Logging"
};

Logger.Write(entry);
}
}


* This source code was highlighted with Source Code Highlighter .

これですべてです。

アプリケーションの起動とロギングの確認


アプリケーションを実行してから、Windowsイベントビューアーを実行して、最新のイベントを監視します。 記録された情報が表示されるはずです。

画像

LogEntryで 重大度を指定することもできます。

LogEntry entry = new LogEntry()
{
Message = "Hello Ent. Lib. Logging" ,
Severity = TraceEventType.Critical
};


* This source code was highlighted with Source Code Highlighter .


ここでアプリケーションを実行すると、重大なWindowsイベントビューアーエラーが表示されます。

PSログブロックに加えて、EntLibには、キャッシング、暗号化、データアクセス、例外処理、ログ、ポリシーインジェクション、セキュリティ、検証、Unityなど、他の多数のブロックが含まれることを自分から追加します。 詳細については、 http://entlib.codeplex.com/をご覧ください

Source: https://habr.com/ru/post/J71078/


All Articles