log format

This commit is contained in:
Timerix 2024-11-03 21:44:03 +05:00
parent 613b11e88c
commit fa9c5ac689
3 changed files with 7 additions and 16 deletions

View File

@ -2,23 +2,16 @@ namespace DTLib.Logging;
public class DefaultLogFormat : ILogFormat public class DefaultLogFormat : ILogFormat
{ {
public bool PrintTimeStamp { get; set; } = true;
public bool PrintTimeStamp { get; set; } public bool PrintContext { get; set; } = true;
public bool PrintContext { get; set; } public bool PrintSeverity { get; set; } = true;
public bool PrintSeverity { get; set; } public string TimeStampFormat { get; set; } = MyTimeFormat.ForText;
public DefaultLogFormat(bool printTimeStamp = true, bool printContext = true, bool printSeverity = true)
{
PrintTimeStamp = printTimeStamp;
PrintContext = printContext;
PrintSeverity = printSeverity;
}
public string CreateMessage(string context, LogSeverity severity, object message) public string CreateMessage(string context, LogSeverity severity, object message)
{ {
var sb = new StringBuilder(); var sb = new StringBuilder();
if (PrintTimeStamp) if (PrintTimeStamp)
sb.Append('[').Append(DateTime.Now.ToString(MyTimeFormat.ForText)).Append(']'); sb.Append('[').Append(DateTime.Now.ToString(TimeStampFormat)).Append(']');
if (PrintContext && PrintSeverity) if (PrintContext && PrintSeverity)
sb.Append('[').Append(context).Append('/').Append(severity.ToString()).Append(']'); sb.Append('[').Append(context).Append('/').Append(severity.ToString()).Append(']');
else if(PrintContext) else if(PrintContext)

View File

@ -2,9 +2,5 @@ namespace DTLib.Logging;
public interface ILogFormat public interface ILogFormat
{ {
bool PrintTimeStamp { get; set; }
bool PrintContext { get; set; }
bool PrintSeverity { get; set; }
string CreateMessage(string context, LogSeverity severity, object message); string CreateMessage(string context, LogSeverity severity, object message);
} }

View File

@ -4,4 +4,6 @@ public static class MyTimeFormat
{ {
public const string ForFileNames="yyyy.MM.dd_HH-mm-ss_zz"; public const string ForFileNames="yyyy.MM.dd_HH-mm-ss_zz";
public const string ForText="yyyy.MM.dd HH:mm:ss zz"; public const string ForText="yyyy.MM.dd HH:mm:ss zz";
public const string TimeOnly="HH:mm:ss";
public const string DateOnly="yyyy.MM.dd";
} }