Windows服务 日志简单实现
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
namespace Service.Util
{
public class FileLogger
{
const string LOG_FILE_NAME = "service.log";
const string DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
static readonly string SERVICE_PATH = GetServiceDirectory();
static FileLogger()
{
try
{
string file_path = Path.Combine(SERVICE_PATH, LOG_FILE_NAME);
Trace.Listeners.Add(new TextWriterTraceListener(file_path));
}
catch //(Exception e)
{
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
}
Trace.AutoFlush = true;
}
#region interface
public void Error(string message)
{
Trace.WriteLine(message, DateTime.Now.ToString(DATETIME_FORMAT) + " ERROR");

本文介绍了如何在Windows服务中实现简单的日志记录功能,包括日志文件的创建、服务运行状态的记录以及错误信息的捕获,为系统监控和故障排查提供帮助。
最低0.47元/天 解锁文章
1328

被折叠的 条评论
为什么被折叠?



