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