Exchange Web Services

ログはデバッグに使用され、アプリケーションに関する作業情報を収集および分析するためにも使用されます。この情報はログと呼ばれるファイルに書き込まれます。ログファイルには、クライアントアプリケーションの操作に関するシステム情報が含まれており、たとえばユーザーまたはプログラムのアクションが含まれます。

この記事では、C# .NETを使用してEWSクライアントのアクティビティログを設定する方法について説明します。

C# .NET APIを使用してMS Exchange Web Servicesで作業する

MS Exchange Web Servicesを管理するために、Aspose.Email for .NETを使用します。

これは、MS Exchange Serverのさまざまなサービスにシームレスにアクセスできる強力なAPIです。また、メールクライアントアプリケーションを実装するための多くの機能を提供します。

APIのDLLをダウンロードするか、以下のコマンドを使用してNuGetからインストールできます。

PM> Install-Package Aspose.Email 

App.configファイルを使用してアクティビティログを有効にする

このオプションは、app.configがアプリケーション設定を保持するための推奨方法であるアプリケーションに適しています。

以下は、C#でEWSClientのログを有効にする手順です。

  • まず、C#プロジェクトにアプリケーション設定ファイルを追加します。まだ追加されていない場合。
  • 次に、設定ファイルに以下の内容を追加します。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="Aspose.Email.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
    <applicationSettings>
        <Aspose.Email.Properties.Settings>
            <setting name="EWSDiagnosticLog" serializeAs="String">
                <value>..\..\..\Log\Aspose.Email.EWS.log</value>
            </setting>
            <setting name="EWSDiagnosticLog_UseDate" serializeAs="String">
                <value>False</value>
            </setting>
        </Aspose.Email.Properties.Settings>
    </applicationSettings>
</configuration>

2つの設定セクションがあります。

  • EWSDiagnosticLog - ログファイルへの相対または絶対パスを指定します。
  • EWSDiagnosticLog_UseDate - ログファイル名に現在の日付の文字列表現を追加するかどうかを指定します。

appsettings.jsonファイルを使用してアクティビティログを有効にする

このオプションは、.NET Coreアプリケーションに推奨されます。

以下は、C#でEWSClientのログを有効にする手順です。

  • まず、C#プロジェクトにappsettings.json設定ファイルを追加します。まだ追加されていない場合。プロジェクトファイルのItemGroupセクションに以下の行が含まれていることを確認してください。
<Content Include="appsettings.json">
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
  • 次に、appsettings.jsonファイルに以下の内容を追加します。
{
  "EWSDiagnosticLog": "ews.log",
  "EWSDiagnosticLog_UseDate": true
}

2つのプロパティがあります。

  • EWSDiagnosticLog - ログファイルへの相対または絶対パスを指定します。
  • EWSDiagnosticLog_UseDate - ログファイル名に現在の日付の文字列表現を追加するかどうかを指定します。

プログラムコードでアクティビティログを有効にする

コード内で即座にログを有効にすることもできます。注意:設定ファイルを使用してすでにログを有効にしている場合でも、このオプションは適用されます。

以下は、C#でEWSClientのログを有効にする手順です。

  • まず、EWSClientを作成します。
  • 次に、LogFileNameプロパティを使用してログファイルへのパスを設定します。
  • 最後に、必要に応じてUseDateInLogFileNameプロパティを設定します。
using (var client = EWSClient.GetEWSClient("https://outlook.office365.com/EWS/Exchange.asmx", credentials))
{
  client.LogFileName = @"Aspose.Email.EWS.log";
  client.UseDateInLogFileName = false;
}

ログファイル情報の例

以下は、ListMessagesメソッドが実行されたときのログファイルエントリの例です。各新しいエントリにはタイムスタンプが付いています。

ログファイルには以下のエントリが区別できます。

  1. ログファイルのヘッダー。Aspose.Emailのバージョン、名前、開始時間が含まれます。
Aspose.Email for .NET [22.10.0.0] EWS Client diagnostic log

Started: 07.11.2022 13:40:16
  1. サーバーへのSOAPリクエスト。
07.11.2022 13:40:16	<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
	<soap:Header>
		<ExchangeImpersonation xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
			<ConnectingSID>
				<SmtpAddress>someaddress@someorg.onmicrosoft.com</SmtpAddress>
			</ConnectingSID>
		</ExchangeImpersonation>
		<RequestServerVersion xmlns="http://schemas.microsoft.com/exchange/services/2006/types" Version="Exchange2013" />
	</soap:Header>
	<soap:Body>
		<FindItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages" Traversal="Shallow">
			<ItemShape>
				<BaseShape xmlns="http://schemas.microsoft.com/exchange/services/2006/types">IdOnly</BaseShape>
			</ItemShape>
			<IndexedPageItemView MaxEntriesReturned="2147483647" Offset="0" BasePoint="Beginning" />
			<ParentFolderIds>
				<DistinguishedFolderId xmlns="http://schemas.microsoft.com/exchange/services/2006/types" Id="inbox" />
			</ParentFolderIds>
		</FindItem>
	</soap:Body>
</soap:Envelope>
  1. HTTPレスポンスヘッダー
07.11.2022 13:40:18	Cache-Control: private
                   	Transfer-Encoding: chunked
                   	Server: Microsoft-IIS/10.0
                   	request-id: 5c777b61-e3d9-c262-fbb0-6f071d9ff68a
                   	Alt-Svc: h3=":443", h3-29=":443"
                   	X-CalculatedFETarget: CH0PR03CU015.internal.outlook.com, BL0PR02CU002.internal.outlook.com
                   	X-BackEndHttpStatus: 200, 200, 200
                   	Set-Cookie: exchangecookie=bf07039c0ee942438170c2958ca2d330; expires=Tue, 07-Nov-2023 10:40:17 GMT; path=/; secure; HttpOnly
                   	X-CalculatedBETarget: BLAPR10MB4915.namprd10.PROD.OUTLOOK.COM
                   	X-RUM-Validated: 1
                   	x-ms-appId: 3fe84e63-c57b-48eb-ab41-879415751cfd
                   	Restrict-Access-Confirm: 1
                   	x-EwsHandler: FindItem
                   	X-AspNet-Version: 4.0.30319
                   	X-BeSku: WCS6
                   	X-DiagInfo: BLAPR10MB4915
                   	X-BEServer: BLAPR10MB4915
                   	X-Proxy-RoutingCorrectness: 1
                   	X-Proxy-BackendServerStatus: 200
                   	X-FEProxyInfo: AS9PR06CA0693.EURPRD06.PROD.OUTLOOK.COM
                   	X-FEEFZInfo: DHR
                   	X-FEServer: BL0PR02CA0063, CH0PR03CA0449, AS9PR06CA0693
                   	X-FirstHopCafeEFZ: DHR
                   	X-Powered-By: ASP.NET
                   	Date: Mon, 07 Nov 2022 10:40:17 GMT
                   	Content-Type: text/xml; charset=utf-8
  1. SOAPサーバーレスポンス。
07.11.2022 13:40:18	<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
	<soap:Header>
		<ExchangeImpersonation xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
			<ConnectingSID>
				<SmtpAddress>someaddress@someorg.onmicrosoft.com</SmtpAddress>
			</ConnectingSID>
		</ExchangeImpersonation>
		<RequestServerVersion Version="Exchange2013" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" />
	</soap:Header>
	<soap:Body>
		<GetItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
			<ItemShape>
				<BaseShape xmlns="http://schemas.microsoft.com/exchange/services/2006/types">IdOnly</BaseShape>
				<AdditionalProperties xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
					<FieldURI FieldURI="item:Attachments" /><FieldURI FieldURI="item:DateTimeCreated" />
					<FieldURI FieldURI="item:DateTimeReceived" /><FieldURI FieldURI="item:DateTimeSent" />
					<FieldURI FieldURI="item:IsDraft" /><FieldURI FieldURI="item:IsFromMe" />
					<FieldURI FieldURI="item:HasAttachments" /><FieldURI FieldURI="item:IsUnmodified" />
					<FieldURI FieldURI="item:ItemClass" /><FieldURI FieldURI="item:IsSubmitted" />
					<FieldURI FieldURI="item:IsResend" /><FieldURI FieldURI="item:DisplayCc" /><FieldURI FieldURI="item:DisplayTo" />
					<FieldURI FieldURI="item:Attachments" /><FieldURI FieldURI="item:LastModifiedTime" />
					<FieldURI FieldURI="item:Size" /><FieldURI FieldURI="item:Subject" />
					<FieldURI FieldURI="item:InternetMessageHeaders" /><FieldURI FieldURI="message:IsRead" />
					<FieldURI FieldURI="message:InternetMessageId" /><FieldURI FieldURI="message:Sender" />
					<FieldURI FieldURI="message:CcRecipients" /><FieldURI FieldURI="message:ToRecipients" />
					<FieldURI FieldURI="message:BccRecipients" /><FieldURI FieldURI="message:From" />
				</AdditionalProperties>
			</ItemShape>
			<ItemIds>
				<ItemId
					Id="AAMkAGJhZjYzY2I5LTdjYWMtNGFmMC05ODI1LTA5MTAzYTgwZTc4OQBGAAAAAABdN1MC60QcSpWwPYUTPhL2BwATlR+p0q0wT6WD0+d4WJhWAAAAAAEMAAATlR+p0q0wT6WD0+d4WJhWAACp2kv8AAA="
					xmlns="http://schemas.microsoft.com/exchange/services/2006/types" />
			</ItemIds>
		</GetItem>
	</soap:Body>
</soap:Envelope>

無料APIライセンスを取得する

評価制限なしでAspose.Email for .NETを使用するための無料の一時ライセンスを取得できます。

まとめ

この記事では、C#を使用してEWSクライアントのアクティビティログを設定する方法を学びました。この機能により、クライアントアプリケーションの監視が向上します。さらに、Aspose.Email for .NETに関する詳細を読むためにドキュメントを探索したり、フォーラムを通じて質問をすることができます。

関連情報