|
// MIT License |
|
// Copyright (c) 2019 litetex |
|
|
|
using System; |
|
using System.Collections.Generic; |
|
using System.IO; |
|
using System.Linq; |
|
using System.Runtime.CompilerServices; |
|
using System.Text; |
|
using System.Threading.Tasks; |
|
|
|
namespace <YourNameSpaceHere> |
|
{ |
|
public static class Log |
|
{ |
|
|
|
private static void SetContext([CallerMemberName] string memberName = "", |
|
[CallerFilePath] string sourceFilePath = "", |
|
[CallerLineNumber] int sourceLineNumber = 0) |
|
{ |
|
Serilog.Log.Logger = Serilog.Log.Logger |
|
.ForContext("MemberName", memberName) |
|
.ForContext("FilePath", sourceFilePath) |
|
.ForContext("FileName", Path.GetFileNameWithoutExtension(sourceFilePath)) |
|
.ForContext("LineNumber", sourceLineNumber); |
|
} |
|
|
|
private static string GetLogString(string message, Exception ex) |
|
{ |
|
return $"{message}: {(ex != null ? ex.ToString() : "")}"; |
|
} |
|
|
|
public static void Debug(string message, [CallerMemberName] string memberName = "", |
|
[CallerFilePath] string sourceFilePath = "", |
|
[CallerLineNumber] int sourceLineNumber = 0) |
|
{ |
|
SetContext(memberName, sourceFilePath, sourceLineNumber); |
|
Serilog.Log.Debug(message); |
|
} |
|
|
|
public static void Debug(string message, Exception ex, [CallerMemberName] string memberName = "", |
|
[CallerFilePath] string sourceFilePath = "", |
|
[CallerLineNumber] int sourceLineNumber = 0) |
|
{ |
|
SetContext(memberName, sourceFilePath, sourceLineNumber); |
|
Serilog.Log.Debug(GetLogString(message, ex)); |
|
} |
|
|
|
public static void Info(string message, [CallerMemberName] string memberName = "", |
|
[CallerFilePath] string sourceFilePath = "", |
|
[CallerLineNumber] int sourceLineNumber = 0) |
|
{ |
|
SetContext(memberName, sourceFilePath, sourceLineNumber); |
|
Serilog.Log.Information(message); |
|
} |
|
|
|
public static void Info(string message, Exception ex, [CallerMemberName] string memberName = "", |
|
[CallerFilePath] string sourceFilePath = "", |
|
[CallerLineNumber] int sourceLineNumber = 0) |
|
{ |
|
SetContext(memberName, sourceFilePath, sourceLineNumber); |
|
Serilog.Log.Information(GetLogString(message, ex)); |
|
} |
|
|
|
public static void Warn(string message, [CallerMemberName] string memberName = "", |
|
[CallerFilePath] string sourceFilePath = "", |
|
[CallerLineNumber] int sourceLineNumber = 0) |
|
{ |
|
SetContext(memberName, sourceFilePath, sourceLineNumber); |
|
Serilog.Log.Warning(message); |
|
} |
|
|
|
public static void Warn(string message, Exception ex, [CallerMemberName] string memberName = "", |
|
[CallerFilePath] string sourceFilePath = "", |
|
[CallerLineNumber] int sourceLineNumber = 0) |
|
{ |
|
SetContext(memberName, sourceFilePath, sourceLineNumber); |
|
Serilog.Log.Warning(GetLogString(message, ex)); |
|
} |
|
|
|
public static void Error(string message, [CallerMemberName] string memberName = "", |
|
[CallerFilePath] string sourceFilePath = "", |
|
[CallerLineNumber] int sourceLineNumber = 0) |
|
{ |
|
SetContext(memberName, sourceFilePath, sourceLineNumber); |
|
Serilog.Log.Error(message); |
|
} |
|
|
|
public static void Error(string message, Exception ex, [CallerMemberName] string memberName = "", |
|
[CallerFilePath] string sourceFilePath = "", |
|
[CallerLineNumber] int sourceLineNumber = 0) |
|
{ |
|
SetContext(memberName, sourceFilePath, sourceLineNumber); |
|
Serilog.Log.Error(GetLogString(message, ex)); |
|
} |
|
|
|
public static void Error(Exception ex, [CallerMemberName] |
|
string memberName = "", |
|
[CallerFilePath] string sourceFilePath = "", |
|
[CallerLineNumber] int sourceLineNumber = 0) |
|
{ |
|
SetContext(memberName, sourceFilePath, sourceLineNumber); |
|
Serilog.Log.Error(ex == null ? ex.ToString() : ""); |
|
} |
|
} |
|
} |
Hi, thanks this script is quite usefull. Just a question though, what does this do in a multithreaded scenario? I could imagine since SetContext is not thread safe there could be an issue where one thread injects the context into the global Log.Logger and then another thread does a writeline, and gets the wrong data.