Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LayoutRenerers added to Logger #17

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions barraider-sdtools/LayoutRenderers/ElapsedTimeLayoutRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using NLog;
using NLog.LayoutRenderers;

namespace BarRaider.SdTools.LayoutRenderers
{
/// <summary>
/// Renderer to get the time elapsed since the last logged event.
/// </summary>
[LayoutRenderer("elapsed-time")]
public class ElapsedTimeLayoutRenderer : LayoutRenderer
{
private DateTime? _lastTimeStamp;

/// <summary>
/// Append elapsed time since the last log entry in milliseconds to the log
/// </summary>
/// <param name="builder">StringBuilder containing log parts, the result is appended to this StringBuilder</param>
/// <param name="logEvent">not used</param>
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
DateTime now = DateTime.Now;
var lastTimeStamp = _lastTimeStamp ?? now;
var elapsedTime = now - lastTimeStamp;
var elapsedTimeString = $"{elapsedTime.TotalMilliseconds:f4}".PadLeft(10);
builder.Append($"{elapsedTimeString}ms");
_lastTimeStamp = now;
}
}

}
27 changes: 27 additions & 0 deletions barraider-sdtools/LayoutRenderers/RealTimeLayoutRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using NLog;
using NLog.LayoutRenderers;

namespace BarRaider.SdTools.LayoutRenderers
{
/// <summary>
/// Renderer to get the real time added to the log. Uses the real time (DateTime.Now) instead of the logEvent.TimeStamp since that TimeStamp seems to be some clustered timestamp used for multiple log entries.
/// </summary>
[LayoutRenderer("real-time")]
public class RealTimeLayoutRenderer : LayoutRenderer
{
/// <summary>
/// Append DateTime.Now including milliseconds to the log
/// </summary>
/// <param name="builder">StringBuilder containing log parts, the result is appended to this StringBuilder</param>
/// <param name="logEvent">not used</param>
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
builder.Append($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
}
}
}
31 changes: 27 additions & 4 deletions barraider-sdtools/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using NLog;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Targets;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -67,14 +70,34 @@ public static Logger Instance
return instance;
}
}
}
}

/// <summary>
/// Set logging format. In addition to the standard layouts defined for NLog, ${real-time} ${elapsed-time} are available.
/// </summary>
/// <example>"https://nlog-project.org/config/?tab=layout-renderers"</example>
public string Layout
{
get
{
LoggingConfiguration lc = NLog.LogManager.Configuration;
return ((FileTarget)lc.LoggingRules[0].Targets[0]).Layout.ToString();
}
set
{
LoggingConfiguration lc = NLog.LogManager.Configuration;
((FileTarget)lc.LoggingRules[0].Targets[0]).Layout = value;
}
}

private readonly NLog.Logger log = null;
private Logger()
{
var config = new NLog.Config.LoggingConfiguration();
var logfile = new NLog.Targets.FileTarget("logfile") { FileName = "pluginlog.log", ArchiveEvery=NLog.Targets.FileArchivePeriod.Day, MaxArchiveFiles=10, ArchiveFileName="logs/log.{###}.log", ArchiveNumbering=NLog.Targets.ArchiveNumberingMode.Rolling };
config.AddRule(LogLevel.Debug, LogLevel.Fatal, logfile);
LayoutRenderer.Register<LayoutRenderers.ElapsedTimeLayoutRenderer>("elapsed-time");
LayoutRenderer.Register<LayoutRenderers.RealTimeLayoutRenderer>("real-time");
var config = new NLog.Config.LoggingConfiguration();
var logfile = new NLog.Targets.FileTarget("logfile") { FileName = "pluginlog.log", ArchiveEvery=NLog.Targets.FileArchivePeriod.Day, MaxArchiveFiles=10, ArchiveFileName="logs/log.{###}.log", ArchiveNumbering=NLog.Targets.ArchiveNumberingMode.Rolling};
config.AddRule(LogLevel.Debug, LogLevel.Fatal, logfile);
NLog.LogManager.Configuration = config;
log = LogManager.GetCurrentClassLogger();
LogMessage(TracingLevel.DEBUG, "Logger Initialized");
Expand Down