The System.Diagnostics namespace provides the foundation for troubleshooting your applications - debugging, event logging, performance monitoring, and interacting with system processes. Here we are concerned about the EventLog class and how we will use it.
For the purposes of this example, we are just going to log any application errors. We are going to be logging these type of entries to the Application Log on the server itself. This is where most applications log their errors, warnings and information.
Example Class:
using System;
using System.Diagnostics;
using System.IO;
namespace System.Diagnostics
{
/// <summary>
/// Summary description for article06012004.
/// </summary>
public class article06012004
{
static void Main(string[] args)
{
MoveDirectory();
}
public static void MoveDirectory()
{
string directoryPathToMove = @"c:\MyDirectory";
string directoryPathMoveLocation = @"c:\MyDirectory1";
try
{
Directory.Move(directoryPathToMove, directoryPathMoveLocation);
}
catch(Exception err)
{
logError(err.ToString(),"ASPAllianceArticle06012004");
}
}
public static void logError(string strError, string strSource)
{
System.Diagnostics.EventLog myEventLog = new System.Diagnostics.EventLog();
myEventLog.Source = strSource;
myEventLog.WriteEntry (strError, System.Diagnostics.EventLogEntryType.Error);
myEventLog.Close();
}
}
}
If an Exception is in fact thrown you can then see this error in your event log.
By using this approach you can code for as much or as little details as your requirements ask for. This way you will have the necessary information to fix any problems that may arise.