JD2022-TU1/main/tools/framework/JD.Perforce/PlumeLoggerAdapter.cs

123 lines
3.5 KiB
C#

using System;
using System.IO;
using System.Reflection;
using JD.Core.Logging;
using JD.Interfaces.Logging;
using Plume.Core.Logging;
namespace JD.Perforce
{
/// <summary>
/// TODO : Maybe move this class to JD.Core if necessary
/// TODO: This class has to be removed asap.
/// Temporary class in order to make the tools that use P4Manager to work again.
/// </summary>
public class PlumeLoggerAdapter : ILogger
{
private readonly FileLog _logger;
public PlumeLoggerAdapter(FileLog logger)
{
_logger = logger;
}
/// <inheritdoc />
public void Debug(string message, Exception exception = null)
{
LogEntry logEntry = new LogEntry
{
Level = LogLevel.Debug,
Message = $"{message} {exception?.Message}",
};
_logger.Log(logEntry);
}
/// <inheritdoc />
public void Error(string message, Exception exception = null)
{
LogEntry logEntry = new LogEntry
{
Level = LogLevel.Error,
Message = $"{message} {exception?.Message}",
};
_logger.Log(logEntry);
}
/// <inheritdoc />
public void Fatal(string message, Exception exception = null)
{
LogEntry logEntry = new LogEntry
{
Level = LogLevel.Fatal,
Message = $"{message} {exception?.Message}",
};
_logger.Log(logEntry);
}
/// <inheritdoc />
public void Info(string message, Exception exception = null)
{
LogEntry logEntry = new LogEntry
{
Level = LogLevel.Message,
Message = $"{message} {exception?.Message}",
};
_logger.Log(logEntry);
}
/// <inheritdoc />
public void Trace(string message, Exception exception = null)
{
LogEntry logEntry = new LogEntry
{
Level = LogLevel.Debug,
Message = $"{message} {exception?.Message}",
};
_logger.Log(logEntry);
}
/// <inheritdoc />
public void Warn(string message, Exception exception = null)
{
LogEntry logEntry = new LogEntry
{
Level = LogLevel.Warning,
Message = $"{message} {exception?.Message}",
};
_logger.Log(logEntry);
}
/// <inheritdoc />
public string Name { get; }
public static PlumeLoggerAdapter Create()
{
string filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "log");
return Create(filePath);
}
public static PlumeLoggerAdapter Create(string path, string fileName = null)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException(nameof(path));
}
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (fileName == null)
{
fileName = $"{AppDomain.CurrentDomain.FriendlyName.Replace('.', '_')}_{DateTime.Now:yyyyMMdd_HHmmss_fff}.txt";
}
string logFilePath = Path.Combine(path, fileName);
FileLog fileLog = new FileLog(logFilePath);
return new PlumeLoggerAdapter(fileLog);
}
}
}