Logging verbose information with OpenPGP Library for .NET

Many command line OpenPGP tools used in production offer a functionality to output verbose information that can be used in case of a failure to identify more thoroughly what went wrong.

As of version 1.7.8.5, DidiSoft OpenPGP Library for .NET also offers an extensive amount of low level log information. In this chapter we are going to illustrate how it can be accessed.

  1. Logging example
  2. Log4Net and Windows Event log

Logging example

In order to access the verbose log messages we have to register an event listener. The example below implements a simple event listener that saves the log messages in a text file.

C# example

using System;
using System.IO;
using DidiSoft.Pgp;
 
public class LogMessagesDemo
{
 public void Demo() 
 {
	PGPLib pgp = new PGPLib();
	pgp.LogMessageEvent += Log_Event;
	// all subsequent operations will be logged
 }
 
 /// This is our event handler where we receive the log messages
 private void Log_Event(object sender, DidiSoft.Pgp.Logging.LogEventArgs e)
 {
	using (StreamWriter log = File.AppendText(@"DataFiles\log.txt")) 
	{
		log.WriteLine(e.EventTime);
		log.WriteLine(e.Message);
 	}
 }
}

VB.NET example

Imports System
Imports System.IO
Imports DidiSoft.Pgp
 
Public Class LogMessagesDemo
 Public Sub Demo()
	Dim pgp As New PGPLib()
	AddHandler pgp.LogMessageEvent, AddressOf Log_Event
	' all subsequent operations will produce verbose log messages
 End Sub
 
 ''' This is our event handler where we receive the log messages
 Private Sub Log_Event(ByVal sender As Object, ByVal e As DidiSoft.Pgp.Logging.LogEventArgs)
	Using log As StreamWriter = File.AppendText("DataFiles\log.txt")
		log.WriteLine(e.EventTime)
		log.WriteLine(e.Message)
	End Using
 End Sub
End Class

Using Log4Net and the Windows Event log

We can write an event handler that can call additional logging frameworks and infrastructures like Log4Net or write to the Windows event log database by invoking the System.Diagnostics.EventLog class.

Summary

This chapter was an introduction to the verbose log information produced by DidiSoft OpenPGP Library for .NET. In order to start receiving low level log information we just have to register an event listener method.

List of methods used

DidiSoft.Pgp.PGPLib.LogMessageEvent – event for receiving verbose log messages