Plane.Libraries/Plane.Logging/LocalFileLogger.cs
zxd 5e5817b8a4 使用通信模块前的提交
修改读取日志
2018-08-11 12:13:56 +08:00

77 lines
2.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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");
}
}
public override string ReadLog()
{
string text = "";
if (File.Exists(LogFilePath))
text = File.ReadAllText(LogFilePath);
return text;
}
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);
}
}
}
}
}
}