Skip to content

Instantly share code, notes, and snippets.

@graealex
Created February 2, 2023 21:44
Show Gist options
  • Save graealex/5e041526c8880691e190902af494ccf1 to your computer and use it in GitHub Desktop.
Save graealex/5e041526c8880691e190902af494ccf1 to your computer and use it in GitHub Desktop.
Serilog BrowserLog Sink
using Serilog.Core;
using System.IO;
using System;
using Serilog.Formatting;
using Serilog.Events;
using System.Runtime.InteropServices.JavaScript;
public partial class BrowserLogSink : ILogEventSink
{
readonly ITextFormatter _formatter;
public BrowserLogSink(ITextFormatter formatter)
{
_formatter = formatter ?? throw new ArgumentNullException(nameof(formatter));
}
public void Emit(LogEvent logEvent)
{
using (var buffer = new StringWriter())
{
_formatter.Format(logEvent, buffer);
switch (logEvent.Level)
{
case LogEventLevel.Information:
ConsoleInfo(buffer.ToString().Trim());
break;
case LogEventLevel.Warning:
ConsoleWarn(buffer.ToString().Trim());
break;
case LogEventLevel.Fatal:
case LogEventLevel.Error:
ConsoleError(buffer.ToString().Trim());
break;
case LogEventLevel.Verbose:
case LogEventLevel.Debug:
ConsoleDebug(buffer.ToString().Trim());
break;
}
}
}
#region Console.Log JS Interop
[JSImport("globalThis.console.log")]
private static partial void _ConsoleLog(
[JSMarshalAs<JSType.String>] string message);
[JSImport("globalThis.console.log")]
private static partial void _ConsoleLog(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0);
[JSImport("globalThis.console.log")]
private static partial void _ConsoleLog(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1);
[JSImport("globalThis.console.log")]
private static partial void _ConsoleLog(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2);
[JSImport("globalThis.console.log")]
private static partial void _ConsoleLog(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3);
[JSImport("globalThis.console.log")]
private static partial void _ConsoleLog(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4);
[JSImport("globalThis.console.log")]
private static partial void _ConsoleLog(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5);
[JSImport("globalThis.console.log")]
private static partial void _ConsoleLog(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5,
[JSMarshalAs<JSType.Any>] object obj6);
[JSImport("globalThis.console.log")]
private static partial void _ConsoleLog(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5,
[JSMarshalAs<JSType.Any>] object obj6,
[JSMarshalAs<JSType.Any>] object obj7);
[JSImport("globalThis.console.log")]
private static partial void _ConsoleLog(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5,
[JSMarshalAs<JSType.Any>] object obj6,
[JSMarshalAs<JSType.Any>] object obj7,
[JSMarshalAs<JSType.Any>] object obj8);
[JSImport("globalThis.console.log")]
private static partial void _ConsoleLog(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5,
[JSMarshalAs<JSType.Any>] object obj6,
[JSMarshalAs<JSType.Any>] object obj7,
[JSMarshalAs<JSType.Any>] object obj8,
[JSMarshalAs<JSType.Any>] object obj9);
private static void ConsoleLog(string message, params object[] objs)
{
switch (objs.Length)
{
case 0:
_ConsoleLog(message);
break;
case 1:
_ConsoleLog(message, objs[0]);
break;
case 2:
_ConsoleLog(message, objs[0], objs[1]);
break;
case 3:
_ConsoleLog(message, objs[0], objs[1], objs[2]);
break;
case 4:
_ConsoleLog(message, objs[0], objs[1], objs[2], objs[3]);
break;
case 5:
_ConsoleLog(message, objs[0], objs[1], objs[2], objs[3], objs[4]);
break;
case 6:
_ConsoleLog(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5]);
break;
case 7:
_ConsoleLog(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5], objs[6]);
break;
case 8:
_ConsoleLog(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5], objs[6], objs[7]);
break;
case 9:
_ConsoleLog(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5], objs[6], objs[7], objs[8]);
break;
case 10:
_ConsoleLog(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5], objs[6], objs[7], objs[8], objs[9]);
break;
default:
throw new System.NotImplementedException();
}
}
#endregion
#region Console.Info JS Interop
[JSImport("globalThis.console.info")]
private static partial void _ConsoleInfo(
[JSMarshalAs<JSType.String>] string message);
[JSImport("globalThis.console.info")]
private static partial void _ConsoleInfo(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0);
[JSImport("globalThis.console.info")]
private static partial void _ConsoleInfo(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1);
[JSImport("globalThis.console.info")]
private static partial void _ConsoleInfo(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2);
[JSImport("globalThis.console.info")]
private static partial void _ConsoleInfo(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3);
[JSImport("globalThis.console.info")]
private static partial void _ConsoleInfo(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4);
[JSImport("globalThis.console.info")]
private static partial void _ConsoleInfo(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5);
[JSImport("globalThis.console.info")]
private static partial void _ConsoleInfo(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5,
[JSMarshalAs<JSType.Any>] object obj6);
[JSImport("globalThis.console.info")]
private static partial void _ConsoleInfo(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5,
[JSMarshalAs<JSType.Any>] object obj6,
[JSMarshalAs<JSType.Any>] object obj7);
[JSImport("globalThis.console.info")]
private static partial void _ConsoleInfo(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5,
[JSMarshalAs<JSType.Any>] object obj6,
[JSMarshalAs<JSType.Any>] object obj7,
[JSMarshalAs<JSType.Any>] object obj8);
[JSImport("globalThis.console.info")]
private static partial void _ConsoleInfo(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5,
[JSMarshalAs<JSType.Any>] object obj6,
[JSMarshalAs<JSType.Any>] object obj7,
[JSMarshalAs<JSType.Any>] object obj8,
[JSMarshalAs<JSType.Any>] object obj9);
private static void ConsoleInfo(string message, params object[] objs)
{
switch (objs.Length)
{
case 0:
_ConsoleInfo(message);
break;
case 1:
_ConsoleInfo(message, objs[0]);
break;
case 2:
_ConsoleInfo(message, objs[0], objs[1]);
break;
case 3:
_ConsoleInfo(message, objs[0], objs[1], objs[2]);
break;
case 4:
_ConsoleInfo(message, objs[0], objs[1], objs[2], objs[3]);
break;
case 5:
_ConsoleInfo(message, objs[0], objs[1], objs[2], objs[3], objs[4]);
break;
case 6:
_ConsoleInfo(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5]);
break;
case 7:
_ConsoleInfo(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5], objs[6]);
break;
case 8:
_ConsoleInfo(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5], objs[6], objs[7]);
break;
case 9:
_ConsoleInfo(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5], objs[6], objs[7], objs[8]);
break;
case 10:
_ConsoleInfo(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5], objs[6], objs[7], objs[8], objs[9]);
break;
default:
throw new System.NotImplementedException();
}
}
#endregion
#region Console.Warn JS Interop
[JSImport("globalThis.console.warn")]
private static partial void _ConsoleWarn(
[JSMarshalAs<JSType.String>] string message);
[JSImport("globalThis.console.warn")]
private static partial void _ConsoleWarn(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0);
[JSImport("globalThis.console.warn")]
private static partial void _ConsoleWarn(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1);
[JSImport("globalThis.console.warn")]
private static partial void _ConsoleWarn(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2);
[JSImport("globalThis.console.warn")]
private static partial void _ConsoleWarn(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3);
[JSImport("globalThis.console.warn")]
private static partial void _ConsoleWarn(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4);
[JSImport("globalThis.console.warn")]
private static partial void _ConsoleWarn(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5);
[JSImport("globalThis.console.warn")]
private static partial void _ConsoleWarn(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5,
[JSMarshalAs<JSType.Any>] object obj6);
[JSImport("globalThis.console.warn")]
private static partial void _ConsoleWarn(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5,
[JSMarshalAs<JSType.Any>] object obj6,
[JSMarshalAs<JSType.Any>] object obj7);
[JSImport("globalThis.console.warn")]
private static partial void _ConsoleWarn(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5,
[JSMarshalAs<JSType.Any>] object obj6,
[JSMarshalAs<JSType.Any>] object obj7,
[JSMarshalAs<JSType.Any>] object obj8);
[JSImport("globalThis.console.warn")]
private static partial void _ConsoleWarn(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5,
[JSMarshalAs<JSType.Any>] object obj6,
[JSMarshalAs<JSType.Any>] object obj7,
[JSMarshalAs<JSType.Any>] object obj8,
[JSMarshalAs<JSType.Any>] object obj9);
private static void ConsoleWarn(string message, params object[] objs)
{
switch (objs.Length)
{
case 0:
_ConsoleWarn(message);
break;
case 1:
_ConsoleWarn(message, objs[0]);
break;
case 2:
_ConsoleWarn(message, objs[0], objs[1]);
break;
case 3:
_ConsoleWarn(message, objs[0], objs[1], objs[2]);
break;
case 4:
_ConsoleWarn(message, objs[0], objs[1], objs[2], objs[3]);
break;
case 5:
_ConsoleWarn(message, objs[0], objs[1], objs[2], objs[3], objs[4]);
break;
case 6:
_ConsoleWarn(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5]);
break;
case 7:
_ConsoleWarn(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5], objs[6]);
break;
case 8:
_ConsoleWarn(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5], objs[6], objs[7]);
break;
case 9:
_ConsoleWarn(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5], objs[6], objs[7], objs[8]);
break;
case 10:
_ConsoleWarn(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5], objs[6], objs[7], objs[8], objs[9]);
break;
default:
throw new System.NotImplementedException();
}
}
#endregion
#region Console.Error JS Interop
[JSImport("globalThis.console.error")]
private static partial void _ConsoleError(
[JSMarshalAs<JSType.String>] string message);
[JSImport("globalThis.console.error")]
private static partial void _ConsoleError(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0);
[JSImport("globalThis.console.error")]
private static partial void _ConsoleError(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1);
[JSImport("globalThis.console.error")]
private static partial void _ConsoleError(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2);
[JSImport("globalThis.console.error")]
private static partial void _ConsoleError(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3);
[JSImport("globalThis.console.error")]
private static partial void _ConsoleError(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4);
[JSImport("globalThis.console.error")]
private static partial void _ConsoleError(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5);
[JSImport("globalThis.console.error")]
private static partial void _ConsoleError(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5,
[JSMarshalAs<JSType.Any>] object obj6);
[JSImport("globalThis.console.error")]
private static partial void _ConsoleError(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5,
[JSMarshalAs<JSType.Any>] object obj6,
[JSMarshalAs<JSType.Any>] object obj7);
[JSImport("globalThis.console.error")]
private static partial void _ConsoleError(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5,
[JSMarshalAs<JSType.Any>] object obj6,
[JSMarshalAs<JSType.Any>] object obj7,
[JSMarshalAs<JSType.Any>] object obj8);
[JSImport("globalThis.console.error")]
private static partial void _ConsoleError(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5,
[JSMarshalAs<JSType.Any>] object obj6,
[JSMarshalAs<JSType.Any>] object obj7,
[JSMarshalAs<JSType.Any>] object obj8,
[JSMarshalAs<JSType.Any>] object obj9);
private static void ConsoleError(string message, params object[] objs)
{
switch (objs.Length)
{
case 0:
_ConsoleError(message);
break;
case 1:
_ConsoleError(message, objs[0]);
break;
case 2:
_ConsoleError(message, objs[0], objs[1]);
break;
case 3:
_ConsoleError(message, objs[0], objs[1], objs[2]);
break;
case 4:
_ConsoleError(message, objs[0], objs[1], objs[2], objs[3]);
break;
case 5:
_ConsoleError(message, objs[0], objs[1], objs[2], objs[3], objs[4]);
break;
case 6:
_ConsoleError(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5]);
break;
case 7:
_ConsoleError(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5], objs[6]);
break;
case 8:
_ConsoleError(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5], objs[6], objs[7]);
break;
case 9:
_ConsoleError(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5], objs[6], objs[7], objs[8]);
break;
case 10:
_ConsoleError(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5], objs[6], objs[7], objs[8], objs[9]);
break;
default:
throw new System.NotImplementedException();
}
}
#endregion
#region Console.Debug JS Interop
[JSImport("globalThis.console.debug")]
private static partial void _ConsoleDebug(
[JSMarshalAs<JSType.String>] string message);
[JSImport("globalThis.console.debug")]
private static partial void _ConsoleDebug(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0);
[JSImport("globalThis.console.debug")]
private static partial void _ConsoleDebug(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1);
[JSImport("globalThis.console.debug")]
private static partial void _ConsoleDebug(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2);
[JSImport("globalThis.console.debug")]
private static partial void _ConsoleDebug(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3);
[JSImport("globalThis.console.debug")]
private static partial void _ConsoleDebug(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4);
[JSImport("globalThis.console.debug")]
private static partial void _ConsoleDebug(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5);
[JSImport("globalThis.console.debug")]
private static partial void _ConsoleDebug(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5,
[JSMarshalAs<JSType.Any>] object obj6);
[JSImport("globalThis.console.debug")]
private static partial void _ConsoleDebug(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5,
[JSMarshalAs<JSType.Any>] object obj6,
[JSMarshalAs<JSType.Any>] object obj7);
[JSImport("globalThis.console.debug")]
private static partial void _ConsoleDebug(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5,
[JSMarshalAs<JSType.Any>] object obj6,
[JSMarshalAs<JSType.Any>] object obj7,
[JSMarshalAs<JSType.Any>] object obj8);
[JSImport("globalThis.console.debug")]
private static partial void _ConsoleDebug(
[JSMarshalAs<JSType.String>] string message,
[JSMarshalAs<JSType.Any>] object obj0,
[JSMarshalAs<JSType.Any>] object obj1,
[JSMarshalAs<JSType.Any>] object obj2,
[JSMarshalAs<JSType.Any>] object obj3,
[JSMarshalAs<JSType.Any>] object obj4,
[JSMarshalAs<JSType.Any>] object obj5,
[JSMarshalAs<JSType.Any>] object obj6,
[JSMarshalAs<JSType.Any>] object obj7,
[JSMarshalAs<JSType.Any>] object obj8,
[JSMarshalAs<JSType.Any>] object obj9);
private static void ConsoleDebug(string message, params object[] objs)
{
switch (objs.Length)
{
case 0:
_ConsoleDebug(message);
break;
case 1:
_ConsoleDebug(message, objs[0]);
break;
case 2:
_ConsoleDebug(message, objs[0], objs[1]);
break;
case 3:
_ConsoleDebug(message, objs[0], objs[1], objs[2]);
break;
case 4:
_ConsoleDebug(message, objs[0], objs[1], objs[2], objs[3]);
break;
case 5:
_ConsoleDebug(message, objs[0], objs[1], objs[2], objs[3], objs[4]);
break;
case 6:
_ConsoleDebug(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5]);
break;
case 7:
_ConsoleDebug(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5], objs[6]);
break;
case 8:
_ConsoleDebug(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5], objs[6], objs[7]);
break;
case 9:
_ConsoleDebug(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5], objs[6], objs[7], objs[8]);
break;
case 10:
_ConsoleDebug(message, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5], objs[6], objs[7], objs[8], objs[9]);
break;
default:
throw new System.NotImplementedException();
}
}
#endregion
}
using System;
using Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Formatting.Display;
using Serilog;
public static class BrowserLogSinkConfigurationExtensions
{
const string DefaultDebugOutputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}";
/// <summary>
/// Writes log events to <see cref="System.Diagnostics.Debug"/>.
/// </summary>
/// <param name="sinkConfiguration">Logger sink configuration.</param>
/// <param name="restrictedToMinimumLevel">The minimum level for
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
/// <param name="levelSwitch">A switch allowing the pass-through minimum level
/// to be changed at runtime.</param>
/// <param name="outputTemplate">A message template describing the format used to write to the sink.
/// the default is <code>"[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"</code>.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <returns>Configuration object allowing method chaining.</returns>
public static LoggerConfiguration BrowserLog(
this LoggerSinkConfiguration sinkConfiguration,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
string outputTemplate = DefaultDebugOutputTemplate,
IFormatProvider? formatProvider = null,
LoggingLevelSwitch? levelSwitch = null)
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
return sinkConfiguration.BrowserLog(formatter, restrictedToMinimumLevel, levelSwitch);
}
/// <summary>
/// Writes log events to <see cref="System.Diagnostics.Debug"/>.
/// </summary>
/// <param name="sinkConfiguration">Logger sink configuration.</param>
/// <param name="formatter">Controls the rendering of log events into text, for example to log JSON. To
/// control plain text formatting, use the overload that accepts an output template.</param>
/// <param name="restrictedToMinimumLevel">The minimum level for
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
/// <param name="levelSwitch">A switch allowing the pass-through minimum level
/// to be changed at runtime.</param>
/// <returns>Configuration object allowing method chaining.</returns>
public static LoggerConfiguration BrowserLog(
this LoggerSinkConfiguration sinkConfiguration,
ITextFormatter formatter,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
LoggingLevelSwitch? levelSwitch = null)
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
return sinkConfiguration.Sink(new BrowserLogSink(formatter), restrictedToMinimumLevel, levelSwitch);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment