69 lines
1.8 KiB
C#
69 lines
1.8 KiB
C#
using System;
|
||
using System.Configuration;
|
||
using System.IO;
|
||
using System.Threading;
|
||
|
||
namespace Plane.Logging
|
||
{
|
||
public class LocalFileLogger : Logger
|
||
{
|
||
private string _logDirPath = Path.Combine(
|
||
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
||
"Plane",
|
||
ConfigurationManager.AppSettings["LogEntry"] ?? AppDomain.CurrentDomain.FriendlyName,
|
||
"Logs"
|
||
);
|
||
|
||
private object _logLock = new object();
|
||
|
||
public LocalFileLogger()
|
||
{
|
||
}
|
||
|
||
public LocalFileLogger(ILogger loggerToDecorate) : base(loggerToDecorate)
|
||
{
|
||
}
|
||
|
||
private string LogDirPath
|
||
{
|
||
get
|
||
{
|
||
if (!Directory.Exists(_logDirPath))
|
||
{
|
||
Directory.CreateDirectory(_logDirPath);
|
||
}
|
||
return _logDirPath;
|
||
}
|
||
}
|
||
|
||
private string LogFilePath
|
||
{
|
||
get
|
||
{
|
||
return Path.Combine(LogDirPath, DateTime.Today.ToString("yyyyMMdd") + ".log");
|
||
}
|
||
}
|
||
|
||
protected override void LogCore(string message, Category category, Priority priority)
|
||
{
|
||
lock (_logLock)
|
||
{
|
||
// TODO: 林俊清,20150605,应改为不使用 AppendAllText,提高性能。
|
||
for (int i = 0; i < 5; i++)
|
||
{
|
||
try
|
||
{
|
||
File.AppendAllText(LogFilePath,
|
||
BuildStandardLogEntry(message, category, priority));
|
||
break;
|
||
}
|
||
catch (IOException)
|
||
{
|
||
Thread.Sleep(100);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|