Configure POP3 Client Activity Logging in .NET Core Applications

Enable Activity Logging in POP3 Client

Use appsettings.json File to Enable Activity Logging

Activity logging is used for debugging, as well as for collecting and analyzing working information about the POP3 client.

NOTE: This option is preferred for .NET Core applications.

Logging in Pop3Client can be enabled with the following steps and code samples:

  1. Add an appsettings.json configuration file to a C# project, if it has not been added before.

  2. Make sure that the project file contains the following lines in the ItemGroup section.

       <Content Include="appsettings.json">
           <CopyToOutputDirectory>Always</CopyToOutputDirectory>
       </Content>
    
  3. Then, add the following content to the appsettings.json file.

       {
         "Pop3DiagnosticLog": "Pop3.log",
         "Pop3DiagnosticLog_UseDate": true
       }
    

The two properties mentioned above are:

  • Pop3DiagnosticLog - specifies the relative or absolute path to the log file.

  • Pop3DiagnosticLog_UseDate - specifies whether to add a string representation of the current date to the log file name.

Use Programm Code to Enable Activity Logging

You can also enable logging immediately in the code.

NOTE: even if you have already enabled logging by using configuration files, this option will be applied.

Logging in Pop3Client can be enabled with the following steps and code samples:

  1. Create an Pop3Client.
  2. Set the path to the log file using the LogFileName property.
  3. Set the UseDateInLogFileName property if it is necessary.
   using (var client = new Pop3Client("your pop3 server", 995, "your username", "your password"))
{
    // Set security mode
    client.SecurityOptions = SecurityOptions.Auto;

    // Set the path to the log file using the LogFileName property.
    client.LogFileName = @"C:\Aspose.Email.Pop3.log";

    // Set the UseDateInLogFileName property if it is necessary.
    client.UseDateInLogFileName = false;
}

Enable Activity Logging with App.config File

Pop3Client activity can be logged by modifying the configSections in the config file. Following are the steps to perform diagnostics logging:

  1. Add a sectionGroup called “applicationSettings”.
  2. Add a section called “Aspose.Email.Properties.Settings”.
  3. Include the setting ImapDiagonosticLog where the file name is defined in the applicationSettings/Aspose.Email.Properties.Settings.

Here is a sample form application which uses Pop3Client to process mail. This whole activity is logged by modifying the App.config file.

  • Create a form based application with a single button on it. Add the following sample code for the button click:
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
Pop3Client client = new Pop3Client("pop.gmail.com", 995, "user@gmail.com", "password");
// Set security mode
client.SecurityOptions = SecurityOptions.Auto;
try
{
// Get the message info collection
Pop3MessageInfoCollection list = client.ListMessages();
// Download each message
for (int i = 0; i < list.Count; i++)
{
// Save the EML file locally
client.SaveMessage(list[i].UniqueId, dataDir + list[i].UniqueId + ".eml");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
  • Add a reference to Aspose.Email.
  • Now add the App.Config file and modify it so that the file contents are as follows:
For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
<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="Pop3DiagnosticLog" serializeAs="String">
<value>Aspose.Email.Pop3.log</value>
</setting>
<setting name="Pop3DiagnosticLog_UseDate" serializeAs="String">
<value>True</value>
</setting>
</Aspose.Email.Properties.Settings>
</applicationSettings>
</configuration>

For C# .NET use the following option

todo:image_alt_text
For VB .NET use the following option
todo:image_alt_text   todo:image_alt_text  
todo:image_alt_text  
  • Run the code and then observe the Log folder. The following file will be generated.
todo:image_alt_text