Skip to content

Commit

Permalink
Added LogCat logging on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
TSchmiedlechner committed Nov 7, 2022
1 parent b3ef1f3 commit 8080a1e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Android.App;
using Android.Content;
using Android.Nfc;
using Android.OS;
using Android.Runtime;
using AndroidX.Core.App;
Expand All @@ -14,6 +15,7 @@
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Formatting.Display;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -40,7 +42,7 @@ public override StartCommandResult OnStartCommand(Intent intent, StartCommandFla
CreateNotificationChannel();

var enableCloseButton = intent.GetBooleanExtra("enableCloseButton", false);
var notification = GetNotification(LauncherState.NotConnected, enableCloseButton) ;
var notification = GetNotification(LauncherState.NotConnected, enableCloseButton);

StartForeground(NOTIFICATION_ID, notification);

Expand Down Expand Up @@ -70,6 +72,7 @@ public override StartCommandResult OnStartCommand(Intent intent, StartCommandFla
Log.Logger = new LoggerConfiguration()
.WriteTo.File(path: Path.Combine(FileLoggerHelper.LogDirectory.FullName, FileLoggerHelper.LogFilename), rollingInterval: RollingInterval.Day, retainedFileCountLimit: 31)
.WriteTo.ApplicationInsights(telemetryConfiguration, TelemetryConverter.Traces, restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Warning)
.WriteTo.Sink(new LogcatSink(AndroidLogger.TAG, logLevel))
.CreateLogger();

Log.Logger.Information("Starting the fiskaltrust.Middleware...");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Microsoft.Extensions.Logging;
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Formatting.Display;
using System;
using System.IO;

namespace fiskaltrust.AndroidLauncher.Common.Helpers.Logging
{
internal class LogcatSink : ILogEventSink
{
private readonly string _tag;
private readonly ITextFormatter _formatter;
private readonly LogEventLevel _restrictedToMinimumLevel;

public LogcatSink(string tag, LogLevel restrictedToMinimumLevel)
{
_tag = tag ?? throw new ArgumentNullException(nameof(tag));

_formatter = new MessageTemplateTextFormatter("{Message:lj}{NewLine}{Exception}");
_restrictedToMinimumLevel = GetSerilogLogLevel(restrictedToMinimumLevel);
}

public void Emit(LogEvent logEvent)
{
if (logEvent.Level < _restrictedToMinimumLevel)
{
return;
}

using var writer = new StringWriter();

_formatter.Format(logEvent, writer);

var msg = writer.ToString();

switch (logEvent.Level)
{
case LogEventLevel.Verbose:
Android.Util.Log.Verbose(_tag, msg);
break;
case LogEventLevel.Debug:
Android.Util.Log.Debug(_tag, msg);
break;
case LogEventLevel.Information:
Android.Util.Log.Info(_tag, msg);
break;
case LogEventLevel.Warning:
Android.Util.Log.Warn(_tag, msg);
break;
case LogEventLevel.Fatal:
case LogEventLevel.Error:
Android.Util.Log.Error(_tag, msg);
break;
}
}

private LogEventLevel GetSerilogLogLevel(LogLevel level)
{
switch (level)
{
case LogLevel.Trace:
return LogEventLevel.Verbose;
case LogLevel.Debug:
return LogEventLevel.Debug;
case LogLevel.Information:
return LogEventLevel.Information;
case LogLevel.Warning:
return LogEventLevel.Warning;
case LogLevel.Error:
return LogEventLevel.Error;
case LogLevel.Critical:
return LogEventLevel.Fatal;
}

return LogEventLevel.Information;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<Compile Include="Helpers\Logging\AndroidLogger.cs" />
<Compile Include="Helpers\Logging\FileLoggerHelper.cs" />
<Compile Include="Helpers\Logging\AndroidLoggerProvider.cs" />
<Compile Include="Helpers\Logging\LogCatSink.cs" />
<Compile Include="Helpers\Logging\MiddlewareTelemetryInitializer.cs" />
<Compile Include="Helpers\Logging\NoopDisposable.cs" />
<Compile Include="Helpers\PowerManagerHelper.cs" />
Expand Down

0 comments on commit 8080a1e

Please sign in to comment.