
Logging được sử dụng để gỡ lỗi, cũng như để thu thập và phân tích thông tin làm việc về ứng dụng. Thông tin này được ghi vào một tệp gọi là log. Các tệp log chứa thông tin hệ thống về hoạt động của ứng dụng khách, ví dụ, hành động của người dùng hoặc chương trình.
Trong bài viết này, chúng ta sẽ đề cập đến cách thiết lập ghi lại hoạt động của EWS client sử dụng C# .NET.
- C# .NET API để làm việc với MS Exchange Web Services
- Kích hoạt ghi lại hoạt động sử dụng tệp App.config
- Kích hoạt ghi lại hoạt động sử dụng tệp appsettings.json
- Kích hoạt ghi lại hoạt động trong mã chương trình
- Một ví dụ về thông tin tệp log
C# .NET API để làm việc với MS Exchange Web Services
Để quản lý MS Exchange Web Services, chúng ta sẽ sử dụng Aspose.Email for .NET.
Đây là một API mạnh mẽ cho phép truy cập vào nhiều dịch vụ của MS Exchange Server một cách liền mạch. Ngoài ra, nó cung cấp nhiều tính năng để triển khai các ứng dụng khách email.
Bạn có thể tải về DLL của API hoặc cài đặt nó từ NuGet bằng cách sử dụng lệnh sau.
PM> Install-Package Aspose.Email
Kích hoạt ghi lại hoạt động sử dụng tệp App.config
Tùy chọn này phù hợp cho các ứng dụng mà app.config
là cách ưa thích để giữ cấu hình ứng dụng.
Sau đây là các bước để kích hoạt ghi lại trong EWSClient bằng C#.
- Đầu tiên, thêm một tệp cấu hình ứng dụng vào dự án C#, nếu nó chưa được thêm trước đó.
- Sau đó, thêm nội dung sau vào tệp cấu hình.
<?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>
Chúng ta có thể thấy hai phần cài đặt:
EWSDiagnosticLog
- Chỉ định đường dẫn tương đối hoặc tuyệt đối đến tệp log.EWSDiagnosticLog_UseDate
- chỉ định xem có thêm một đại diện chuỗi của ngày hiện tại vào tên tệp log hay không.
Kích hoạt ghi lại hoạt động sử dụng tệp appsettings.json
Tùy chọn này được ưa thích cho các ứng dụng .NET Core.
Sau đây là các bước để kích hoạt ghi lại trong EWSClient bằng C#.
- Đầu tiên, thêm một tệp cấu hình
appsettings.json
vào dự án C#, nếu nó chưa được thêm trước đó. Đảm bảo rằng tệp dự án chứa các dòng sau trong phần ItemGroup:
<Content Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
- Sau đó, thêm nội dung sau vào tệp appsettings.json.
{
"EWSDiagnosticLog": "ews.log",
"EWSDiagnosticLog_UseDate": true
}
Chúng ta có thể thấy hai thuộc tính:
EWSDiagnosticLog
- Chỉ định đường dẫn tương đối hoặc tuyệt đối đến tệp log.EWSDiagnosticLog_UseDate
- chỉ định xem có thêm một đại diện chuỗi của ngày hiện tại vào tên tệp log hay không.
Kích hoạt ghi lại hoạt động trong mã chương trình
Bạn cũng có thể kích hoạt ghi lại ngay lập tức trong mã. Lưu ý: ngay cả khi bạn đã kích hoạt ghi lại bằng cách sử dụng các tệp cấu hình, tùy chọn này sẽ được áp dụng.
Sau đây là các bước để kích hoạt ghi lại trong EWSClient bằng C#.
- Đầu tiên, tạo một EWSClient.
- Thứ hai, đặt đường dẫn đến tệp log bằng cách sử dụng thuộc tính LogFileName.
- Cuối cùng, đặt thuộc tính UseDateInLogFileName nếu cần thiết.
using (var client = EWSClient.GetEWSClient("https://outlook.office365.com/EWS/Exchange.asmx", credentials))
{
client.LogFileName = @"Aspose.Email.EWS.log";
client.UseDateInLogFileName = false;
}
Một ví dụ về thông tin tệp log
Dưới đây là một ví dụ về các mục tệp log khi phương thức ListMessages được thực thi. Mỗi mục mới đều được trước bởi một dấu thời gian.
Các mục sau có thể được phân biệt trong tệp log:
- Tiêu đề tệp log. Bao gồm phiên bản Aspose.Email, tên và thời gian bắt đầu.
Aspose.Email for .NET [22.10.0.0] EWS Client diagnostic log
Started: 07.11.2022 13:40:16
- Yêu cầu SOAP đến máy chủ.
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>
- Các tiêu đề phản hồi 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
- Phản hồi SOAP từ máy chủ.
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>
Nhận Giấy phép API Miễn phí
Bạn có thể nhận giấy phép tạm thời miễn phí để sử dụng Aspose.Email for .NET mà không có giới hạn đánh giá.
Kết luận
Trong bài viết này, bạn đã học cách thiết lập ghi lại hoạt động trong EWS client sử dụng C#. Tính năng này cho phép bạn theo dõi tốt hơn ứng dụng khách của mình. Ngoài ra, bạn có thể khám phá tài liệu để đọc thêm về Aspose.Email for .NET. Ngoài ra, bạn có thể đặt câu hỏi của mình qua diễn đàn.