Plane.Libraries/Plane.Logging/LocalFileLogger.cs

77 lines
2.0 KiB
C#
Raw Permalink Normal View History

2017-02-27 02:04:13 +08:00
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");
}
}
2018-08-03 11:43:10 +08:00
public override string ReadLog()
{
string text = "";
if (File.Exists(LogFilePath))
text = File.ReadAllText(LogFilePath);
2018-08-03 11:43:10 +08:00
return text;
}
2017-02-27 02:04:13 +08:00
protected override void LogCore(string message, Category category, Priority priority)
{
lock (_logLock)
{
2020-09-20 11:42:25 +08:00
// TODO: 王海20150605应改为不使用 AppendAllText提高性能。
2017-02-27 02:04:13 +08:00
for (int i = 0; i < 5; i++)
{
try
{
File.AppendAllText(LogFilePath,
BuildStandardLogEntry(message, category, priority));
break;
}
catch (IOException)
{
Thread.Sleep(100);
}
}
}
}
}
}