1
This commit is contained in:
52
Src/Unity/Scripts/LoggerForUnity.cs
Normal file
52
Src/Unity/Scripts/LoggerForUnity.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using UnityEngine;
|
||||
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
public sealed class UnityLogger:ILogger
|
||||
{
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
|
||||
{
|
||||
switch (logLevel)
|
||||
{
|
||||
case LogLevel.Critical:
|
||||
case LogLevel.Error:
|
||||
if (exception is not null)
|
||||
{
|
||||
Debug.LogException(exception);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"{CurrentScope}:{state.ToString()}");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Debug.Log($"{CurrentScope}:{state.ToString()}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
public bool IsEnabled(LogLevel logLevel) => true;
|
||||
private string CurrentScope { get; set; }
|
||||
public IDisposable BeginScope<TState>(TState state) where TState : notnull
|
||||
{
|
||||
CurrentScope = typeof(TState).Name;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public sealed class UnityLoggerProvider:ILoggerProvider
|
||||
{
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
public ILogger CreateLogger(string categoryName)
|
||||
{
|
||||
return new UnityLogger();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user