Created
February 25, 2025 15:39
-
-
Save davepermen/e94da365586accd7504d9f803fe79ebc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class ErrorLogger | |
{ | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
static internal void LogError(string message, params object[] parameters) => logError(message, parameters); | |
internal delegate void LogErrorDelegate(string message, params object[] parameters); | |
static readonly LogErrorDelegate logError = DontLogError; | |
private static void DontLogError(string message, params object[] parameters) { } | |
static ErrorLogger() | |
{ | |
if (AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).FirstOrDefault(t => t.FullName == "Serilog.Log") is Type type) | |
{ | |
var loggerInstance = type.GetProperty("Logger")?.GetValue(null) ?? null; | |
var errorMethod = loggerInstance?.GetType().GetMethod("Error", [typeof(string), typeof(object[])]); | |
logError = (errorMethod?.CreateDelegate(typeof(LogErrorDelegate), loggerInstance) as LogErrorDelegate) ?? logError; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment