Refactor logs for structured logging and add new Value struct:
- Refactor various logs to enable automagic structured logging - Add `Value` struct for creating values with debug expressions - Update `LogEvent` constructor to include an expression field - Modify log methods to pass debug expressions for messages - Implement new methods for logging values and objects with debug expressions
This commit is contained in:
parent
0619812089
commit
0fce863a81
143
logging/Log.cs
143
logging/Log.cs
@ -11,13 +11,77 @@ using System.Linq;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Security.Cryptography.X509Certificates;
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using lib.Net;
|
using lib.Net;
|
||||||
|
using System.Dynamic;
|
||||||
//using System.Threading.Tasks;
|
//using System.Threading.Tasks;
|
||||||
|
|
||||||
#nullable enable
|
#nullable enable
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
T O D O :
|
||||||
|
x) Refactor various logs in order to do automagic structured logging
|
||||||
|
x)
|
||||||
|
|
||||||
|
|
||||||
|
D O N E:
|
||||||
|
|
||||||
|
|
||||||
|
N O T D O I N G :
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
public record struct Value<T>( T _val = default, string _exp = "" )
|
||||||
|
{
|
||||||
|
|
||||||
|
static public Value<T> Get<T>( T v,
|
||||||
|
[CallerArgumentExpression("fn")]
|
||||||
|
string dbgExp = ""
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return new( v, dbgExp );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct SourceLoc
|
||||||
|
{
|
||||||
|
readonly string _reason = "";
|
||||||
|
readonly string _dbgName = "";
|
||||||
|
readonly string _dbgPath = "";
|
||||||
|
readonly int _dbgLine = -1;
|
||||||
|
|
||||||
|
public SourceLoc( string reason, string dbgName, string dbgPath, int dbgLine )
|
||||||
|
{
|
||||||
|
_reason = reason;
|
||||||
|
_dbgName = dbgName;
|
||||||
|
_dbgPath = dbgPath;
|
||||||
|
_dbgLine = dbgLine;
|
||||||
|
}
|
||||||
|
|
||||||
|
static public SourceLoc Record(
|
||||||
|
string reason = "",
|
||||||
|
[CallerMemberName] string dbgName = "",
|
||||||
|
[CallerFilePath] string dbgPath = "",
|
||||||
|
[CallerLineNumber] int dbgLine = 0
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return new SourceLoc( reason, dbgName, dbgPath, dbgLine );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static public class log
|
static public class log
|
||||||
{
|
{
|
||||||
|
|
||||||
|
static public Value<T> Value<T>( T val,
|
||||||
|
[CallerArgumentExpression("val")]
|
||||||
|
string dbgExp = ""
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return new( val, dbgExp );
|
||||||
|
}
|
||||||
|
|
||||||
[Flags]
|
[Flags]
|
||||||
public enum LogType
|
public enum LogType
|
||||||
@ -55,6 +119,7 @@ static public class log
|
|||||||
public string Path;
|
public string Path;
|
||||||
public int Line;
|
public int Line;
|
||||||
public string Member;
|
public string Member;
|
||||||
|
public string Exp;
|
||||||
|
|
||||||
public string Cat;
|
public string Cat;
|
||||||
public object? Obj;
|
public object? Obj;
|
||||||
@ -64,7 +129,7 @@ static public class log
|
|||||||
static ImmutableDictionary<int, string> s_shortname = ImmutableDictionary<int, string>.Empty;
|
static ImmutableDictionary<int, string> s_shortname = ImmutableDictionary<int, string>.Empty;
|
||||||
|
|
||||||
|
|
||||||
public LogEvent( LogType logType, string msg, string path, int line, string member, string cat, object? obj )
|
public LogEvent( LogType logType, string msg, string path, int line, string member, string cat, string exp, object? obj )
|
||||||
{
|
{
|
||||||
|
|
||||||
//Cache the automatic category names
|
//Cache the automatic category names
|
||||||
@ -100,12 +165,13 @@ static public class log
|
|||||||
Line = line;
|
Line = line;
|
||||||
Member = member;
|
Member = member;
|
||||||
Cat = cat;
|
Cat = cat;
|
||||||
|
Exp = exp;
|
||||||
Obj = obj;
|
Obj = obj;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
static LogEvent CreateLogEvent( LogType logType, string msg, string cat, object? obj, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" )
|
static LogEvent CreateLogEvent( LogType logType, string msg, string cat, object? obj, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "", string exp = "" )
|
||||||
{
|
{
|
||||||
var logEvent = new LogEvent( logType, msg, path, line, member, cat, obj );
|
var logEvent = new LogEvent( logType, msg, path, line, member, cat, exp, obj );
|
||||||
|
|
||||||
return logEvent;
|
return logEvent;
|
||||||
}
|
}
|
||||||
@ -199,52 +265,69 @@ static public class log
|
|||||||
#endregion // Util
|
#endregion // Util
|
||||||
|
|
||||||
#region Forwards
|
#region Forwards
|
||||||
static public void fatal( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" )
|
static public void fatal( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "", [CallerArgumentExpression("msg")] string dbgExp = "" )
|
||||||
{
|
{
|
||||||
logBase( msg, LogType.Fatal, path, line, member, cat, obj );
|
logBase( msg, LogType.Fatal, path, line, member, cat, dbgExp, obj );
|
||||||
}
|
}
|
||||||
|
|
||||||
static public void error( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" )
|
static public void error( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "", [CallerArgumentExpression("msg")] string dbgExp = "" )
|
||||||
{
|
{
|
||||||
logBase( msg, LogType.Error, path, line, member, cat, obj );
|
logBase( msg, LogType.Error, path, line, member, cat, dbgExp, obj );
|
||||||
}
|
}
|
||||||
|
|
||||||
static public void warn( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" )
|
static public void warn( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "", [CallerArgumentExpression("msg")] string dbgExp = "" )
|
||||||
{
|
{
|
||||||
logBase( msg, LogType.Warn, path, line, member, cat, obj );
|
logBase( msg, LogType.Warn, path, line, member, cat, dbgExp, obj );
|
||||||
}
|
}
|
||||||
|
|
||||||
static public void high( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" )
|
static public void high( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "", [CallerArgumentExpression("msg")] string dbgExp = "" )
|
||||||
{
|
{
|
||||||
logBase( msg, LogType.High, path, line, member, cat, obj );
|
logBase( msg, LogType.High, path, line, member, cat, dbgExp, obj );
|
||||||
}
|
}
|
||||||
|
|
||||||
static public void info( string msg, string cat = "", object? obj = null,
|
static public void info( string msg, string cat = "", object? obj = null,
|
||||||
[CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" )
|
[CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "", [CallerArgumentExpression("msg")] string dbgExp = "" )
|
||||||
{
|
{
|
||||||
logBase( msg, LogType.Info, path, line, member, cat, obj );
|
logBase( msg, LogType.Info, path, line, member, cat, dbgExp, obj );
|
||||||
}
|
}
|
||||||
|
|
||||||
static public void debug( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" )
|
static public void debug( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "", [CallerArgumentExpression("msg")] string dbgExp = "" )
|
||||||
{
|
{
|
||||||
logBase( msg, LogType.Debug, path, line, member, cat, obj );
|
logBase( msg, LogType.Debug, path, line, member, cat, dbgExp, obj );
|
||||||
}
|
}
|
||||||
|
|
||||||
static public void trace( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" )
|
static public void trace( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "", [CallerArgumentExpression("msg")] string dbgExp = "" )
|
||||||
{
|
{
|
||||||
logBase( msg, LogType.Trace, path, line, member, cat, obj );
|
logBase( msg, LogType.Trace, path, line, member, cat, dbgExp, obj );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static public void inf_valueo<A>( string msg, Value<A> v1, string cat = "" )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
static public void info<A>( string msg, A a, string cat = "", [CallerArgumentExpression("a")] string dbgExpA = "" )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
static public void info( string msg, object a, object b, string cat = "", [CallerArgumentExpression("a")] string dbgExpA = "", [CallerArgumentExpression("a")] string dbgExpB = "" )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Helpers
|
#region Helpers
|
||||||
static public void logProps( object obj, string header, LogType type = LogType.Debug, string cat = "", string prefix = "", [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" )
|
static public void logProps( object obj, string header, LogType type = LogType.Debug, string cat = "", string prefix = "", [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "", [CallerArgumentExpression("obj")] string dbgExpObj = "" )
|
||||||
{
|
{
|
||||||
var list = refl.GetAllProperties( obj.GetType() );
|
var list = refl.GetAllProperties( obj.GetType() );
|
||||||
|
|
||||||
lock( s_lock )
|
lock( s_lock )
|
||||||
{
|
{
|
||||||
var evt = new LogEvent( type, header, path, line, member, cat, obj );
|
var evt = new LogEvent( type, header, path, line, member, cat, dbgExpObj, obj );
|
||||||
{
|
{
|
||||||
|
|
||||||
s_events.Enqueue( evt );
|
s_events.Enqueue( evt );
|
||||||
@ -255,11 +338,11 @@ static public class log
|
|||||||
{
|
{
|
||||||
var v = pi.GetValue( obj );
|
var v = pi.GetValue( obj );
|
||||||
|
|
||||||
logBase( $"{prefix}{pi.Name} = {v}", type, path, line, member, cat );
|
logBase( $"{prefix}{pi.Name} = {v}", type, path, line, member, dbgExpObj, cat );
|
||||||
}
|
}
|
||||||
catch( Exception ex )
|
catch( Exception ex )
|
||||||
{
|
{
|
||||||
logBase( $"Exception processing {pi.Name} {ex.Message}", LogType.Error, "log" );
|
logBase( $"Exception processing {pi.Name} {ex.Message}", LogType.Error, path, line, member, cat, dbgExpObj, obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -284,16 +367,16 @@ static public class log
|
|||||||
|
|
||||||
static object s_lock = new object();
|
static object s_lock = new object();
|
||||||
|
|
||||||
static public LogEvent logCreateEvent( string msg, LogType type = LogType.Debug, string path = "", int line = -1, string member = "", string cat = "unk", object? obj = null )
|
static public LogEvent logCreateEvent( string msg, LogType type = LogType.Debug, string path = "", int line = -1, string member = "", string cat = "unk", string exp = "", object? obj = null )
|
||||||
{
|
{
|
||||||
LogEvent evt = new LogEvent( type, msg, path, line, member, cat, obj );
|
LogEvent evt = new LogEvent( type, msg, path, line, member, cat, exp, obj );
|
||||||
return evt;
|
return evt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static public void logBase( string msg, LogType type = LogType.Debug, string path = "", int line = -1, string member = "", string cat = "unk", object? obj = null )
|
static public void logBase( string msg, LogType type = LogType.Debug, string path = "", int line = -1, string member = "", string cat = "unk", string exp = "", object? obj = null )
|
||||||
{
|
{
|
||||||
var evt = logCreateEvent( msg, type, path, line, member, cat );
|
var evt = logCreateEvent( msg, type, path, line, member, cat, exp );
|
||||||
|
|
||||||
s_events.Enqueue( evt );
|
s_events.Enqueue( evt );
|
||||||
}
|
}
|
||||||
@ -365,10 +448,10 @@ static public class log
|
|||||||
{
|
{
|
||||||
var time = DateTime.Now;
|
var time = DateTime.Now;
|
||||||
// Header for this run
|
// Header for this run
|
||||||
var blankLine = new LogEvent( LogType.Raw, $"", "", 0, "", "lib.time", null );
|
var blankLine = new LogEvent( LogType.Raw, $"", "", 0, "", "lib.time", "", null );
|
||||||
var beginLine = new LogEvent( LogType.Raw, $"Begin B E G I N ******************************************************************************************************************", "", 0, "", "lib.time", null );
|
var beginLine = new LogEvent( LogType.Raw, $"Begin B E G I N ******************************************************************************************************************", "", 0, "", "lib.time", "", null );
|
||||||
var endLine = new LogEvent( LogType.Raw, $"End E N D ******************************************************************************************************************", "", 0, "", "lib.time", null );
|
var endLine = new LogEvent( LogType.Raw, $"End E N D ******************************************************************************************************************", "", 0, "", "lib.time", "", null );
|
||||||
var timeLine = new LogEvent( LogType.Raw, $"D A T E {time.Year}/{time.Month.ToString("00")}/{time.Day.ToString("00")} T I M E {time.Hour.ToString("00")}:{time.Minute.ToString("00")}:{time.Second.ToString("00")}.{time.Millisecond.ToString("000")}{time.Microsecond.ToString("000")}", "", 0, "", "lib.time", null );
|
var timeLine = new LogEvent( LogType.Raw, $"D A T E {time.Year}/{time.Month.ToString("00")}/{time.Day.ToString("00")} T I M E {time.Hour.ToString("00")}:{time.Minute.ToString("00")}:{time.Second.ToString("00")}.{time.Millisecond.ToString("000")}{time.Microsecond.ToString("000")}", "", 0, "", "lib.time", "", null );
|
||||||
|
|
||||||
|
|
||||||
//writeToAll( endLine );
|
//writeToAll( endLine );
|
||||||
@ -572,7 +655,7 @@ static public class log
|
|||||||
{
|
{
|
||||||
s_lastDisplaySeconds = curSeconds;
|
s_lastDisplaySeconds = curSeconds;
|
||||||
|
|
||||||
var minuteEvt = new LogEvent( LogType.Raw, $"T I M E ==> {evt.Time.Hour.ToString("00")}:{evt.Time.Minute.ToString("00")}:{evt.Time.Second.ToString("00")}.{evt.Time.Millisecond.ToString("000")} : {evt.Time.ToShortDateString()}", "", 0, "", "lib.time", null );
|
var minuteEvt = new LogEvent( LogType.Raw, $"T I M E ==> {evt.Time.Hour.ToString("00")}:{evt.Time.Minute.ToString("00")}:{evt.Time.Second.ToString("00")}.{evt.Time.Millisecond.ToString("000")} : {evt.Time.ToShortDateString()}", "", 0, "", "lib.time", "", null );
|
||||||
minuteEvt.Time = evt.Time;
|
minuteEvt.Time = evt.Time;
|
||||||
writeSpecialEvent( minuteEvt );
|
writeSpecialEvent( minuteEvt );
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user