Skip to content

Commit

Permalink
Add appcenter scope & event tracking instead of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Dec 19, 2023
1 parent 1149e86 commit 49ee67d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
4 changes: 3 additions & 1 deletion samples/Sample.Maui/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ static MauiAppBuilder RegisterLogging(this MauiAppBuilder builder)
builder.Logging.AddDebug();
#endif
builder.Logging.AddSqlite(Path.Combine(FileSystem.AppDataDirectory, "logging.db"), LogLevel.Debug);
//builder.Logging.AddAppCenter("")
#if !MACCATALYST
builder.Logging.AddAppCenter("TEST");
#endif

return builder;
}
Expand Down
8 changes: 3 additions & 5 deletions samples/Sample.Maui/Sample.Maui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,13 @@

<PropertyGroup Condition="$(TargetFramework.Contains('-ios'))">
<SupportedOSPlatformVersion>14.2</SupportedOSPlatformVersion>

<RuntimeIdentifier>ios-arm64</RuntimeIdentifier>

<!--
<RuntimeIdentifier>ios-arm64</RuntimeIdentifier>
<RuntimeIdentifier>iossimulator-x64</RuntimeIdentifier>
-->
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-ios|AnyCPU'">
<CreatePackage>false</CreatePackage>
</PropertyGroup>
<ItemGroup>
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />
Expand Down Expand Up @@ -74,6 +71,7 @@
<ProjectReference Include="..\..\src\Shiny.SpeechRecognition\Shiny.SpeechRecognition.csproj" />
<ProjectReference Include="..\..\src\Shiny.Notifications\Shiny.Notifications.csproj" />
<ProjectReference Include="..\..\src\Shiny.Logging.Sqlite\Shiny.Logging.Sqlite.csproj" />
<ProjectReference Include="..\..\src\Shiny.Logging.AppCenter\Shiny.Logging.AppCenter.csproj" Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) != 'maccatalyst'" />
</ItemGroup>

<!--
Expand Down
47 changes: 38 additions & 9 deletions src/Shiny.Logging.AppCenter/AppCenterLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,61 @@ namespace Shiny.Logging.AppCenter;
public class AppCenterLogger : ILogger
{
readonly string categoryName;
readonly IExternalScopeProvider? scopeProvider;
readonly LogLevel configLogLevel;


public AppCenterLogger(string categoryName, LogLevel logLevel)
public AppCenterLogger(string categoryName, LogLevel logLevel, IExternalScopeProvider? scopeProvider)
{
this.categoryName = categoryName;
this.configLogLevel = logLevel;
this.scopeProvider = scopeProvider;
}

public IDisposable? BeginScope<TState>(TState state) where TState : notnull
=> this.scopeProvider?.Push(state);

public IDisposable BeginScope<TState>(TState state) => NullScope.Instance;
public bool IsEnabled(LogLevel logLevel) => logLevel >= this.configLogLevel;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
if (!this.IsEnabled(logLevel))
return;

var message = formatter(state, exception);
exception ??= new Exception(message);
Crashes.TrackError(
exception,
new Dictionary<string, string>
var message = formatter(state, exception);
var scopeVars = new Dictionary<string, string>
{
{ "Message", message }
};

this.scopeProvider?.ForEachScope((scope, dict) =>
{
if (scope is IEnumerable<KeyValuePair<string, object>> properties)
{
foreach (var pair in properties)
{
dict.Add(pair.Key, pair.Value.ToString() ?? String.Empty);
}
}
else if (scope != null)
{
{ "Message", message }
dict.Add("Scope", scope.ToString()!);
}
);
}, scopeVars);

if (logLevel <= LogLevel.Information || exception == null)
{
Analytics.TrackEvent(
this.categoryName,
scopeVars
);
}
else
{
Crashes.TrackError(
exception,
scopeVars
);
}
}
}
#endif
9 changes: 7 additions & 2 deletions src/Shiny.Logging.AppCenter/AppCenterLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
namespace Shiny.Logging.AppCenter;


public class AppCenterLoggerProvider : ILoggerProvider
public class AppCenterLoggerProvider : ILoggerProvider, ISupportExternalScope
{
readonly LogLevel logLevel;
IExternalScopeProvider? scopeProvider;

public AppCenterLoggerProvider(LogLevel logLevel) => this.logLevel = logLevel;


public ILogger CreateLogger(string categoryName) => new AppCenterLogger(categoryName, this.logLevel);
public ILogger CreateLogger(string categoryName) => new AppCenterLogger(categoryName, this.logLevel, this.scopeProvider);
public void Dispose() { }

public void SetScopeProvider(IExternalScopeProvider scopeProvider)
=> this.scopeProvider = scopeProvider;
}
#endif

0 comments on commit 49ee67d

Please sign in to comment.