Fix: Additional properties are ignored

This commit is contained in:
lukeIam committed 2018-01-28 14:17:47 +01:00
1 parent 7f2f5dd2d2
commit 5b61bc4bd2
3 files changed
+296 -2

No files matched your search

+2 -2
View File
@@ -111,8 +111,8 @@ namespace VocaluxeLib.Log
#endif #endif
.CreateLogger(); .CreateLogger();
// Adding fist line to log with information about this run // Adding first line to log with information about this run
Information("Starting to log", Params( new { Version = _CurrentVersion, StartDate = DateTime.Now, Id = Guid.NewGuid() } ) ); Information("Starting to log", Params( new { Version = _CurrentVersion}, new { StartDate = DateTime.Now}, new { Id = Guid.NewGuid() } ) );
} }
/// <summary> /// <summary>
+267
View File
@@ -17,7 +17,9 @@
using System; using System;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using Serilog.Context; using Serilog.Context;
using Serilog.Core; using Serilog.Core;
@@ -25,6 +27,7 @@ namespace VocaluxeLib.Log
{ {
public static partial class CLog public static partial class CLog
{ {
private static Regex _PropertiesRegex = new Regex("{[^}]+}");
public static object[] Params(params object[] propertyValues) public static object[] Params(params object[] propertyValues)
{ {
@@ -66,9 +69,21 @@ namespace VocaluxeLib.Log
public static void Verbose(string messageTemplate, object[] propertyValues, bool show = false, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Verbose(string messageTemplate, object[] propertyValues, bool show = false, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_MainLog.Verbose(messageTemplate, propertyValues); _MainLog.Verbose(messageTemplate, propertyValues);
} }
}
else
{
_MainLog.Verbose(messageTemplate, propertyValues);
}
}
if(show) if(show)
ShowLogAssistant(messageTemplate, propertyValues); ShowLogAssistant(messageTemplate, propertyValues);
} }
@@ -107,9 +122,21 @@ namespace VocaluxeLib.Log
public static void Verbose(Exception exception, string messageTemplate, object[] propertyValues, bool show = false, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Verbose(Exception exception, string messageTemplate, object[] propertyValues, bool show = false, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_MainLog.Verbose(exception, messageTemplate, propertyValues); _MainLog.Verbose(exception, messageTemplate, propertyValues);
} }
}
else
{
_MainLog.Verbose(exception, messageTemplate, propertyValues);
}
}
if(show) if(show)
ShowLogAssistant(messageTemplate, propertyValues); ShowLogAssistant(messageTemplate, propertyValues);
} }
@@ -150,9 +177,21 @@ namespace VocaluxeLib.Log
public static void Debug(string messageTemplate, object[] propertyValues, bool show = false, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Debug(string messageTemplate, object[] propertyValues, bool show = false, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_MainLog.Debug(messageTemplate, propertyValues); _MainLog.Debug(messageTemplate, propertyValues);
} }
}
else
{
_MainLog.Debug(messageTemplate, propertyValues);
}
}
if(show) if(show)
ShowLogAssistant(messageTemplate, propertyValues); ShowLogAssistant(messageTemplate, propertyValues);
} }
@@ -191,9 +230,21 @@ namespace VocaluxeLib.Log
public static void Debug(Exception exception, string messageTemplate, object[] propertyValues, bool show = false, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Debug(Exception exception, string messageTemplate, object[] propertyValues, bool show = false, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_MainLog.Debug(exception, messageTemplate, propertyValues); _MainLog.Debug(exception, messageTemplate, propertyValues);
} }
}
else
{
_MainLog.Debug(exception, messageTemplate, propertyValues);
}
}
if(show) if(show)
ShowLogAssistant(messageTemplate, propertyValues); ShowLogAssistant(messageTemplate, propertyValues);
} }
@@ -234,9 +285,21 @@ namespace VocaluxeLib.Log
public static void Information(string messageTemplate, object[] propertyValues, bool show = false, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Information(string messageTemplate, object[] propertyValues, bool show = false, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_MainLog.Information(messageTemplate, propertyValues); _MainLog.Information(messageTemplate, propertyValues);
} }
}
else
{
_MainLog.Information(messageTemplate, propertyValues);
}
}
if(show) if(show)
ShowLogAssistant(messageTemplate, propertyValues); ShowLogAssistant(messageTemplate, propertyValues);
} }
@@ -275,9 +338,21 @@ namespace VocaluxeLib.Log
public static void Information(Exception exception, string messageTemplate, object[] propertyValues, bool show = false, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Information(Exception exception, string messageTemplate, object[] propertyValues, bool show = false, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_MainLog.Information(exception, messageTemplate, propertyValues); _MainLog.Information(exception, messageTemplate, propertyValues);
} }
}
else
{
_MainLog.Information(exception, messageTemplate, propertyValues);
}
}
if(show) if(show)
ShowLogAssistant(messageTemplate, propertyValues); ShowLogAssistant(messageTemplate, propertyValues);
} }
@@ -318,9 +393,21 @@ namespace VocaluxeLib.Log
public static void Warning(string messageTemplate, object[] propertyValues, bool show = false, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Warning(string messageTemplate, object[] propertyValues, bool show = false, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_MainLog.Warning(messageTemplate, propertyValues); _MainLog.Warning(messageTemplate, propertyValues);
} }
}
else
{
_MainLog.Warning(messageTemplate, propertyValues);
}
}
if(show) if(show)
ShowLogAssistant(messageTemplate, propertyValues); ShowLogAssistant(messageTemplate, propertyValues);
} }
@@ -359,9 +446,21 @@ namespace VocaluxeLib.Log
public static void Warning(Exception exception, string messageTemplate, object[] propertyValues, bool show = false, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Warning(Exception exception, string messageTemplate, object[] propertyValues, bool show = false, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_MainLog.Warning(exception, messageTemplate, propertyValues); _MainLog.Warning(exception, messageTemplate, propertyValues);
} }
}
else
{
_MainLog.Warning(exception, messageTemplate, propertyValues);
}
}
if(show) if(show)
ShowLogAssistant(messageTemplate, propertyValues); ShowLogAssistant(messageTemplate, propertyValues);
} }
@@ -402,9 +501,21 @@ namespace VocaluxeLib.Log
public static void Error(string messageTemplate, object[] propertyValues, bool show = false, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Error(string messageTemplate, object[] propertyValues, bool show = false, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_MainLog.Error(messageTemplate, propertyValues); _MainLog.Error(messageTemplate, propertyValues);
} }
}
else
{
_MainLog.Error(messageTemplate, propertyValues);
}
}
if(show) if(show)
ShowLogAssistant(messageTemplate, propertyValues); ShowLogAssistant(messageTemplate, propertyValues);
} }
@@ -443,9 +554,21 @@ namespace VocaluxeLib.Log
public static void Error(Exception exception, string messageTemplate, object[] propertyValues, bool show = false, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Error(Exception exception, string messageTemplate, object[] propertyValues, bool show = false, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_MainLog.Error(exception, messageTemplate, propertyValues); _MainLog.Error(exception, messageTemplate, propertyValues);
} }
}
else
{
_MainLog.Error(exception, messageTemplate, propertyValues);
}
}
if(show) if(show)
ShowLogAssistant(messageTemplate, propertyValues); ShowLogAssistant(messageTemplate, propertyValues);
} }
@@ -490,9 +613,21 @@ namespace VocaluxeLib.Log
public static void Fatal(string messageTemplate, object[] propertyValues, bool show = true, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Fatal(string messageTemplate, object[] propertyValues, bool show = true, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_MainLog.Fatal(messageTemplate, propertyValues); _MainLog.Fatal(messageTemplate, propertyValues);
} }
}
else
{
_MainLog.Fatal(messageTemplate, propertyValues);
}
}
if(show) if(show)
ShowLogAssistant(messageTemplate, propertyValues, true, false); ShowLogAssistant(messageTemplate, propertyValues, true, false);
@@ -539,9 +674,21 @@ namespace VocaluxeLib.Log
public static void Fatal(Exception exception, string messageTemplate, object[] propertyValues, bool show = true, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Fatal(Exception exception, string messageTemplate, object[] propertyValues, bool show = true, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_MainLog.Fatal(exception, messageTemplate, propertyValues); _MainLog.Fatal(exception, messageTemplate, propertyValues);
} }
}
else
{
_MainLog.Fatal(exception, messageTemplate, propertyValues);
}
}
if(show) if(show)
ShowLogAssistant(messageTemplate, propertyValues, true, false); ShowLogAssistant(messageTemplate, propertyValues, true, false);
@@ -586,10 +733,22 @@ namespace VocaluxeLib.Log
public static void Verbose(string messageTemplate, object[] propertyValues, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Verbose(string messageTemplate, object[] propertyValues, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_SongLog.Verbose(messageTemplate, propertyValues); _SongLog.Verbose(messageTemplate, propertyValues);
} }
} }
else
{
_SongLog.Verbose(messageTemplate, propertyValues);
}
}
}
/// <summary> /// <summary>
/// Write an event with the Verbose level and associated exception to the Verbose log. /// Write an event with the Verbose level and associated exception to the Verbose log.
@@ -621,10 +780,22 @@ namespace VocaluxeLib.Log
public static void Verbose(Exception exception, string messageTemplate, object[] propertyValues, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Verbose(Exception exception, string messageTemplate, object[] propertyValues, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_SongLog.Verbose(exception, messageTemplate, propertyValues); _SongLog.Verbose(exception, messageTemplate, propertyValues);
} }
} }
else
{
_SongLog.Verbose(exception, messageTemplate, propertyValues);
}
}
}
#endregion #endregion
@@ -658,10 +829,22 @@ namespace VocaluxeLib.Log
public static void Debug(string messageTemplate, object[] propertyValues, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Debug(string messageTemplate, object[] propertyValues, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_SongLog.Debug(messageTemplate, propertyValues); _SongLog.Debug(messageTemplate, propertyValues);
} }
} }
else
{
_SongLog.Debug(messageTemplate, propertyValues);
}
}
}
/// <summary> /// <summary>
/// Write an event with the Debug level and associated exception to the Debug log. /// Write an event with the Debug level and associated exception to the Debug log.
@@ -693,10 +876,22 @@ namespace VocaluxeLib.Log
public static void Debug(Exception exception, string messageTemplate, object[] propertyValues, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Debug(Exception exception, string messageTemplate, object[] propertyValues, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_SongLog.Debug(exception, messageTemplate, propertyValues); _SongLog.Debug(exception, messageTemplate, propertyValues);
} }
} }
else
{
_SongLog.Debug(exception, messageTemplate, propertyValues);
}
}
}
#endregion #endregion
@@ -730,10 +925,22 @@ namespace VocaluxeLib.Log
public static void Information(string messageTemplate, object[] propertyValues, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Information(string messageTemplate, object[] propertyValues, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_SongLog.Information(messageTemplate, propertyValues); _SongLog.Information(messageTemplate, propertyValues);
} }
} }
else
{
_SongLog.Information(messageTemplate, propertyValues);
}
}
}
/// <summary> /// <summary>
/// Write an event with the Information level and associated exception to the Information log. /// Write an event with the Information level and associated exception to the Information log.
@@ -765,10 +972,22 @@ namespace VocaluxeLib.Log
public static void Information(Exception exception, string messageTemplate, object[] propertyValues, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Information(Exception exception, string messageTemplate, object[] propertyValues, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_SongLog.Information(exception, messageTemplate, propertyValues); _SongLog.Information(exception, messageTemplate, propertyValues);
} }
} }
else
{
_SongLog.Information(exception, messageTemplate, propertyValues);
}
}
}
#endregion #endregion
@@ -802,10 +1021,22 @@ namespace VocaluxeLib.Log
public static void Warning(string messageTemplate, object[] propertyValues, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Warning(string messageTemplate, object[] propertyValues, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_SongLog.Warning(messageTemplate, propertyValues); _SongLog.Warning(messageTemplate, propertyValues);
} }
} }
else
{
_SongLog.Warning(messageTemplate, propertyValues);
}
}
}
/// <summary> /// <summary>
/// Write an event with the Warning level and associated exception to the Warning log. /// Write an event with the Warning level and associated exception to the Warning log.
@@ -837,10 +1068,22 @@ namespace VocaluxeLib.Log
public static void Warning(Exception exception, string messageTemplate, object[] propertyValues, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Warning(Exception exception, string messageTemplate, object[] propertyValues, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_SongLog.Warning(exception, messageTemplate, propertyValues); _SongLog.Warning(exception, messageTemplate, propertyValues);
} }
} }
else
{
_SongLog.Warning(exception, messageTemplate, propertyValues);
}
}
}
#endregion #endregion
@@ -874,10 +1117,22 @@ namespace VocaluxeLib.Log
public static void Error(string messageTemplate, object[] propertyValues, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Error(string messageTemplate, object[] propertyValues, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_SongLog.Error(messageTemplate, propertyValues); _SongLog.Error(messageTemplate, propertyValues);
} }
} }
else
{
_SongLog.Error(messageTemplate, propertyValues);
}
}
}
/// <summary> /// <summary>
/// Write an event with the Error level and associated exception to the Error log. /// Write an event with the Error level and associated exception to the Error log.
@@ -909,10 +1164,22 @@ namespace VocaluxeLib.Log
public static void Error(Exception exception, string messageTemplate, object[] propertyValues, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void Error(Exception exception, string messageTemplate, object[] propertyValues, [CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
_SongLog.Error(exception, messageTemplate, propertyValues); _SongLog.Error(exception, messageTemplate, propertyValues);
} }
} }
else
{
_SongLog.Error(exception, messageTemplate, propertyValues);
}
}
}
#endregion #endregion
} }
+27
View File
@@ -39,7 +39,9 @@
using System; using System;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using Serilog.Context; using Serilog.Context;
using Serilog.Core; using Serilog.Core;
@@ -47,6 +49,7 @@ namespace VocaluxeLib.Log
{ {
public static partial class CLog public static partial class CLog
{ {
private static Regex _PropertiesRegex = new Regex("{[^}]+}");
public static object[] Params(params object[] propertyValues) public static object[] Params(params object[] propertyValues)
{ {
@@ -110,9 +113,21 @@ namespace VocaluxeLib.Log
public static void <#= LogLevel #>(string messageTemplate, object[] propertyValues, <#= Log == "Main"?(LogLevel == "Fatal"?"bool show = true, ":"bool show = false, "):"" #>[CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void <#= LogLevel #>(string messageTemplate, object[] propertyValues, <#= Log == "Main"?(LogLevel == "Fatal"?"bool show = true, ":"bool show = false, "):"" #>[CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
<#= Log == "Main"?"_MainLog":"_SongLog" #>.<#= LogLevel #>(messageTemplate, propertyValues); <#= Log == "Main"?"_MainLog":"_SongLog" #>.<#= LogLevel #>(messageTemplate, propertyValues);
} }
}
else
{
<#= Log == "Main"?"_MainLog":"_SongLog" #>.<#= LogLevel #>(messageTemplate, propertyValues);
}
}
<# if(Log == "Main") { #> <# if(Log == "Main") { #>
if(show) if(show)
ShowLogAssistant(messageTemplate, propertyValues<#= LogLevel == "Fatal"?", true, false":"" #>); ShowLogAssistant(messageTemplate, propertyValues<#= LogLevel == "Fatal"?", true, false":"" #>);
@@ -171,9 +186,21 @@ namespace VocaluxeLib.Log
public static void <#= LogLevel #>(Exception exception, string messageTemplate, object[] propertyValues, <#= Log == "Main"?(LogLevel == "Fatal"?"bool show = true, ":"bool show = false, "):"" #>[CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1) public static void <#= LogLevel #>(Exception exception, string messageTemplate, object[] propertyValues, <#= Log == "Main"?(LogLevel == "Fatal"?"bool show = true, ":"bool show = false, "):"" #>[CallerMemberName] string callerMethodeName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumer = -1)
{ {
using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer})) using (LogContext.PushProperty("CallingContext", new { callerMethodeName, callerFilePath, callerLineNumer}))
{
int usedPropertiesCount = _PropertiesRegex.Matches(messageTemplate).Cast<Match>().Select(m => m.Value).Distinct().Count();
if (propertyValues.Length > usedPropertiesCount)
{
using (LogContext.PushProperty("AdditionalData", propertyValues.Skip(usedPropertiesCount)))
{ {
<#= Log == "Main"?"_MainLog":"_SongLog" #>.<#= LogLevel #>(exception, messageTemplate, propertyValues); <#= Log == "Main"?"_MainLog":"_SongLog" #>.<#= LogLevel #>(exception, messageTemplate, propertyValues);
} }
}
else
{
<#= Log == "Main"?"_MainLog":"_SongLog" #>.<#= LogLevel #>(exception, messageTemplate, propertyValues);
}
}
<# if(Log == "Main") { #> <# if(Log == "Main") { #>
if(show) if(show)
ShowLogAssistant(messageTemplate, propertyValues<#= LogLevel == "Fatal"?", true, false":"" #>); ShowLogAssistant(messageTemplate, propertyValues<#= LogLevel == "Fatal"?", true, false":"" #>);