
日志记录用于调试,以及收集和分析有关应用程序的工作信息。此信息被写入一个称为日志的文件。 日志文件包含有关客户端应用程序操作的系统信息,例如用户或程序操作。
在本文中,我们将介绍 如何使用 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 日志记录的步骤。
- 首先,如果尚未添加,则向 C# 项目添加
appsettings.json
配置文件。确保项目文件在 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 的信息。您还可以通过我们的 论坛 提出问题。