.NET 的可视化消息日志记录代码库






4.43/5 (3投票s)
消息日志记录代码库例程允许您将应用程序中的消息输出到文本窗口、文本文件或 Windows 事件日志。
- 下载 pfMessageLogsProjectVS2010.zip - 1.6 MB
- 下载 pfMessageLogsBinaries.zip - 53.5 KB
- 下载 pfMessageLogsHelpFile.zip - 298.2 KB
- 下载 pfMessageLogsSource.zip - 62 KB
引言
作为我正在进行的 Windows 和 .NET 编程工作的一部分,我经常发现需要提供某种日志记录或审计功能。
在几种情况下,需要日志文件或者简单地使用日志文件非常有用
- 为长时间运行的代码片段提供中间结果或状态消息。
- 以可以保存到外部文本文件的形式提供结果。
- 保存消息,这些消息记录处理过程中预期和非预期事件。
- 为应用程序提供审计跟踪(启动、停止等)。
该库通过提供对消息的构建、格式化、显示和保存方式的更精细的控制,取代了 Windows 和 .NET 编程中使用的Console.WriteLine、MessageBox和TextOut功能。
日志记录被打包成三组类
- MessageLog 类为您的消息提供一个可视化输出窗口。
- TextLogFile 类提供了一种简单的方法来格式化直接写入文件的消息。
- WindowsEventLog 类封装了写入 Windows 事件日志所需的逻辑。
应用程序消息的可视化日志窗口
MessageLog 代码库例程允许您将应用程序中的消息输出到文本窗口。您可以直接从输出窗口搜索、格式化、打印和保存消息。您可以控制窗口是否从您的应用程序可见。
//
// Visual Message Log Code Sample
//
public MessageLog _messageLog;
.....
_messageLog = new MessageLog();
_messageLog.Caption = "Application Message Log for TestprogMessageLogs";
_messageLog.ShowDatetime = false;
_messageLog.ShowWindow();
.....
.....
private long GetSum(long minNum, long maxNum, long outputEveryInterval, bool showDateTime)
{
long sum = 0;
try
{
_messageLog.Clear();
_messageLog.ShowDatetime = showDateTime;
_messageLog.WriteLine("Running GetSum routine ...");
_messageLog.ShowWindow();
for (long i = minNum; i <= maxNum; i++)
{
sum += i;
if ((i % outputEveryInterval) == 0 || i == maxNum)
{
_msg.Length = 0;
_msg.Append("Sum calculated to " + i.ToString("#,##0"));
_msg.Append(" = ");
_msg.Append(sum.ToString("#,##0"));
_messageLog.WriteLine(_msg.ToString());
}
}
}
catch (System.Exception ex)
{
_msg.Length = 0;
_msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
_messageLog.WriteLine(_msg.ToString());
}
finally
{
;
}
return sum;
}
.....
可视化消息日志输出窗口
文本文件日志
您可以将格式化的应用程序消息写入文本日志文件。输出消息可以格式化为自动包含日期/时间、消息类型(错误、警告、信息和警报)、发送消息的应用程序名称和生成消息的计算机名称。
文本日志类没有可视化用户界面。您的代码直接写入外部文件。
//
// TextLogFile sample code
//
.....
private long GetSumToOutputFile(string outputFilename, bool appendMessagesIfFileExists,
bool showMessageType, bool showApplicationName,
bool showMachineName,long minNum, long maxNum,
long outputEveryInterval, bool showDateTime)
{
long sum = 0;
TextLogFile logfile = null;
try
{
logfile = new TextLogFile(outputFilename);
logfile.ShowMessageType = showMessageType;
if(showApplicationName)
{
logfile.ApplicationName = "TestprogMessageLogs";
}
if(showMachineName)
{
logfile.MachineName = Environment.MachineName;
}
if(File.Exists(outputFilename))
{
if(appendMessagesIfFileExists == false)
{
logfile.TruncateFile();
}
}
logfile.ShowDatetime = showDateTime;
for (long i = minNum; i <= maxNum; i++)
{
sum += i;
if ((i % outputEveryInterval) == 0 || i == maxNum)
{
_msg.Length = 0;
_msg.Append("Sum calculated to " + i.ToString("#,##0"));
_msg.Append(" = ");
_msg.Append(sum.ToString("#,##0"));
logfile.WriteLine(_msg.ToString(), TextLogFile.LogMessageType.Information);
}
}
if (showMessageType)
{
logfile.WriteLine("This is a test warning message.", TextLogFile.LogMessageType.Warning);
logfile.WriteLine("This is a test error message.", TextLogFile.LogMessageType.Error);
}
}
catch (System.Exception ex)
{
_msg.Length = 0;
_msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
logfile.WriteLine(_msg.ToString(), TextLogFile.LogMessageType.Error);
}
finally
{
;
}
return sum;
}
包含日志条目的示例文本文件
Windows 事件日志
您可以将格式化的应用程序消息写入任何 Windows 内置事件日志(应用程序、系统、安全和设置)。
//
// WindowsEventLog sample code
//
public MessageLog _messageLog;
.....
_messageLog = new MessageLog();
_messageLog.Caption = "Application Message Log for TestprogMessageLogs";
_messageLog.ShowDatetime = false;
_messageLog.ShowWindow();
.....
.....
public void OutputMessagesToWindowsApplicationEventLog(string eventSourceName,
int numInformationMessagesToWrite,
int numWarningMessagesToWrite,
int numErrorMessagesToWrite)
{
WindowsEventLog eventLog = null;
try
{
if (WindowsEventLog.SourceExists(eventSourceName) == false)
{
WindowsEventLog.CreateEventSource(eventSourceName);
if (WindowsEventLog.SourceExists(eventSourceName))
{
_msg.Length = 0;
_msg.Append("Event Source ");
_msg.Append(eventSourceName);
_msg.Append(" create succeeded.");
WriteMessageToLog(_msg.ToString());
}
else
{
_msg.Length = 0;
_msg.Append("Event Source ");
_msg.Append(eventSourceName);
_msg.Append(" create failed.");
WriteMessageToLog(_msg.ToString());
}
}
else
{
_msg.Length = 0;
_msg.Append("Event Source ");
_msg.Append(eventSourceName);
_msg.Append(" exists.");
WriteMessageToLog(_msg.ToString());
}
eventLog = new WindowsEventLog(WindowsEventLog.EventLogName.Application, ".", eventSourceName);
for (int i = 1; i <= numInformationMessagesToWrite; i++)
{
_msg.Length = 0;
_msg.Append("Message ");
_msg.Append(i.ToString());
_msg.Append(" from test program.");
eventLog.WriteEntry(_msg.ToString(), WindowsEventLog.WindowsEventLogEntryType.Information);
}
for (int i = 1; i <= numWarningMessagesToWrite; i++)
{
_msg.Length = 0;
_msg.Append("Warning message ");
_msg.Append(i.ToString());
_msg.Append(" from test program.");
eventLog.WriteEntry(_msg.ToString(), WindowsEventLog.WindowsEventLogEntryType.Warning);
}
for (int i = 1; i <= numErrorMessagesToWrite; i++)
{
_msg.Length = 0;
_msg.Append("Error message ");
_msg.Append(i.ToString());
_msg.Append(" from test program.");
eventLog.WriteEntry(_msg.ToString(), WindowsEventLog.WindowsEventLogEntryType.Error);
}
_msg.Length = 0;
_msg.Append("Number of event log messages written = ");
_msg.Append((numInformationMessagesToWrite + numWarningMessagesToWrite + numErrorMessagesToWrite).ToString("#,##0"));
WriteMessageToLog(_msg.ToString());
}
catch (System.Exception ex)
{
_msg.Length = 0;
_msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
_msg.Append(Environment.NewLine);
_msg.Append("Caller must have elevated security permissions (e.g. use Run As Administrator) to create and delete event sources and event logs.");
WriteMessageToLog(_msg.ToString());
}
finally
{
;
}
}
.....
//calling module must set MessageLog property to a valid instance of the MessageLog class for messages to be displayed
public void WriteMessageToLog(string msg)
{
if (_messageLog != null)
_messageLog.WriteLine(msg);
}
上述示例代码写入的应用程序事件日志条目
关注点
MessageLog 类允许您轻松构建可视化日志输出窗口。此类在测试程序代码时特别有用,但也对于为交付的应用程序提供易于实现的状态/输出窗口非常有用。
示例代码中包含其他库。这些额外的库被测试程序使用,该测试程序演示了如何使用各种库例程。这些额外的库是通用 pfCodeLibrary 项目的一部分。有关这些帮助器类的文档和代码示例可以在 CodePlex 上的 pfCodeLibrary 项目中找到,网址为https://pfcodelibrary.codeplex.com/。
历史
本文最初于 2016 年 7 月 6 日发表在 CodeProject 上。
本文于 2016 年 7 月 7 日重新发布,以修复输出窗口的显示。
此代码库的版本以前发布在 CodePlex 上,网址为