Logging to device (Android.Util.Log/CoreFoundation.OSLog) #26711
Unanswered
MartinRobins
asked this question in
Ideas
Replies: 1 comment
-
if you are trying to log to Device Log you can do the following Android public class AndroidLoggerProvider : ILoggerProvider
{
ILogger ILoggerProvider.CreateLogger(string categoryName)
{
// Category name is often the full class name, like MyApp.ViewModel.MyViewModel This removes
// the namespace:
int lastDotPos = categoryName.LastIndexOf('.');
if (lastDotPos > 0)
{
categoryName = categoryName[(lastDotPos + 1)..];
}
return new AndroidLogger(categoryName);
}
void IDisposable.Dispose() => GC.SuppressFinalize(this);
}
public class AndroidLogger(string category) : ILogger
{
IDisposable ILogger.BeginScope<TState>(TState state) => null!;
bool ILogger.IsEnabled(LogLevel logLevel) => true;
void ILogger.Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
string message = formatter(state, exception);
Java.Lang.Throwable? throwable = null;
if (exception is not null)
{
throwable = Java.Lang.Throwable.FromException(exception);
}
switch (logLevel)
{
case LogLevel.Trace:
Android.Util.Log.Verbose(category, throwable, message);
break;
case LogLevel.Debug:
Android.Util.Log.Debug(category, throwable, message);
break;
case LogLevel.Information:
Android.Util.Log.Info(category, throwable, message);
break;
case LogLevel.Warning:
Android.Util.Log.Warn(category, throwable, message);
break;
case LogLevel.Error:
Android.Util.Log.Error(category, throwable, message);
break;
case LogLevel.Critical:
Android.Util.Log.Wtf(category, throwable, message);
break;
}
}
} iOS public class iOSLoggerProvider : ILoggerProvider
{
public ILogger CreateLogger(string categoryName)
{
// Category name is often the full class name, like MyApp.ViewModel.MyViewModel This removes
// the namespace:
int lastDotPos = categoryName.LastIndexOf('.');
if (lastDotPos > 0)
{
categoryName = categoryName[(lastDotPos + 1)..];
}
return new iOSLogger(categoryName);
}
public void Dispose() => GC.SuppressFinalize(this);
}
public class iOSLogger : ILogger
{
public iOSLogger(string category)
{
_category = category;
}
private readonly string _category;
public IDisposable BeginScope<TState>(TState state) => null!;
public bool IsEnabled(LogLevel logLevel) => true;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
string message = formatter(state, exception);
switch (logLevel)
{
case LogLevel.Trace:
case LogLevel.Debug:
CoreFoundation.OSLog.Default.Log(OSLogLevel.Debug, $"{_category}: {message}");
break;
case LogLevel.Information:
CoreFoundation.OSLog.Default.Log(OSLogLevel.Info, $"{_category}: {message}");
break;
case LogLevel.Warning:
CoreFoundation.OSLog.Default.Log(OSLogLevel.Default, $"{_category}: {message}");
break;
case LogLevel.Error:
CoreFoundation.OSLog.Default.Log(OSLogLevel.Error, $"{_category}: {message}");
break;
case LogLevel.Critical:
CoreFoundation.OSLog.Default.Log(OSLogLevel.Fault, $"{_category}: {message}");
break;
}
}
} Usage ILoggerProvider loggerProvider = ServiceProvider.GetRequiredService<ILoggerProvider>(); // Or constructor injection
ILogger logger = loggerProvider.CreateLogger(nameof(System.Diagnostics.Activity));
logger?.LogError("Message to Log"); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Unless it already exists (in which case I cannot find it), it would be useful to add ILogging providers for Android.Util.Log and CoreFoundation.OSLog so that anything written to the ILogger<> can be picked up in the device logs.
Beta Was this translation helpful? Give feedback.
All reactions