
日誌 用於除錯,以及收集和分析有關應用程式的工作資訊。這些資訊寫入名為日誌的檔案中。 日誌檔案包含有關客戶端應用程式操作的系統資訊,例如用戶或程式的行為。
在本文中,我們將介紹 如何使用 C# .NET 設定 EWS 客戶端的活動日誌。
- C# .NET API 使用 MS Exchange Web Services
- 使用 App.config 檔案啟用活動日誌
- 使用 appsettings.json 檔案啟用活動日誌
- 在程式碼中啟用活動日誌
- 日誌檔案資訊的範例
C# .NET API 使用 MS Exchange Web Services
要管理 MS Exchange Web Services,我們將使用 Aspose.Email for .NET。
這是一個強大的 API,可以無縫訪問 MS Exchange Server 的各種服務。此外,它提供了許多功能來實現電子郵件客戶端應用程式。
您可以選擇 下載 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>
我們可以看到兩個設定區域:
EWSDiagnosticLog
- 指定日誌檔案的相對或絕對路徑。EWSDiagnosticLog_UseDate
- 指定是否將當前日期的字串表示形式添加到日誌檔案名稱中。
使用 appsettings.json 檔案啟用活動日誌
此選項適用於 .NET Core 應用程式。
以下是在 C# 中啟用 EWSClient 日誌的步驟。
- 首先,將
appsettings.json
配置檔案添加到 C# 專案中,如果尚未添加。確保專案檔案的 ItemGroup 區域包含以下行:
<Content Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
- 然後,將以下內容添加到 appsettings.json 檔案中。
{
"EWSDiagnosticLog": "ews.log",
"EWSDiagnosticLog_UseDate": true
}
我們可以看到兩個屬性:
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 方法時日誌檔案條目的範例。每個新條目都有時間戳。
在日誌檔案中可以區分以下條目:
- 日誌檔案標頭。包括 Aspose.Email 版本、名稱和啟動時間。
Aspose.Email for .NET [22.10.0.0] EWS Client diagnostic log
Started: 07.11.2022 13:40:16
- 發送到伺服器的 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>
- 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
- 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 的資訊。您也可以通過我們的 論壇 提出問題。