Skip to content

Commit

Permalink
Create LoggerFactory in specific impl
Browse files Browse the repository at this point in the history
Added `Debug.Log` provider
  • Loading branch information
ShortDevelopment committed Jan 6, 2025
1 parent e1c9934 commit 652173a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Extensions.Logging;
using ShortDev.Microsoft.ConnectedDevices.Encryption;
using ShortDev.Microsoft.ConnectedDevices.Encryption;
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
Expand All @@ -14,14 +13,4 @@ public static X509Certificate2 CreateDeviceCertificate([NotNull] CdpEncryptionPa
CertificateRequest certRequest = new("CN=Ms-Cdp", key, HashAlgorithmName.SHA256);
return certRequest.CreateSelfSigned(DateTimeOffset.Now, DateTimeOffset.Now.AddYears(5));
}

public static ILoggerFactory CreateLoggerFactory(string filePattern, LogLevel logLevel = LogLevel.Debug)
=> LoggerFactory.Create(builder =>
{
builder.ClearProviders();

builder.SetMinimumLevel(logLevel);

builder.AddFile(filePattern, logLevel);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.CorrelationVector" Version="1.0.42" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0" />
<PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="Bond.CSharp" Version="10.0.0" />
</ItemGroup>

Expand Down
3 changes: 3 additions & 0 deletions src/NearShare.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
<AndroidPackageFormat>aab</AndroidPackageFormat>
</PropertyGroup>
<ItemGroup Label="Nuget">
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.0" />
<PackageReference Include="Rive.Android" Version="1.0.5" />
<PackageReference Include="Sentry" Version="5.0.0" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0" />
<PackageReference Include="ShortDev.Android" Version="0.1.0-beta" />
<PackageReference Include="ShortDev.Android.FluentIcons" Version="1.1.203" />
<PackageReference Include="Xamarin.AndroidX.AppCompat" Version="1.6.1.8" />
Expand Down
2 changes: 1 addition & 1 deletion src/ReceiveActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected override void OnCreate(Bundle? savedInstanceState)

FindViewById<Button>(Resource.Id.openFAQButton)!.Click += (s, e) => UIHelper.OpenFAQ(this);

_loggerFactory = ConnectedDevicesPlatform.CreateLoggerFactory(this.GetLogFilePattern());
_loggerFactory = CdpUtils.CreateLoggerFactory(this);
_logger = _loggerFactory.CreateLogger<ReceiveActivity>();

UIHelper.RequestReceivePermissions(this);
Expand Down
2 changes: 1 addition & 1 deletion src/SendActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected override void OnCreate(Bundle? savedInstanceState)
);
DeviceDiscoveryListView.SetAdapter(adapterDescriptor.CreateRecyclerViewAdapter(RemoteSystems));

_loggerFactory = ConnectedDevicesPlatform.CreateLoggerFactory(this.GetLogFilePattern());
_loggerFactory = CdpUtils.CreateLoggerFactory(this);
_logger = _loggerFactory.CreateLogger<SendActivity>();

UIHelper.RequestSendPermissions(this);
Expand Down
17 changes: 17 additions & 0 deletions src/Utils/CdpUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ namespace NearShare.Utils;

internal static class CdpUtils
{
public static ILoggerFactory CreateLoggerFactory(Context context, LogLevel logLevel = LogLevel.Debug)
{
var pattern = context.GetLogFilePattern();

return LoggerFactory.Create(builder =>
{
builder.ClearProviders();

builder.SetMinimumLevel(logLevel);

#if DEBUG
builder.AddDebug();
#endif
builder.AddFile(context.GetLogFilePattern(), logLevel);
});
}

public static ConnectedDevicesPlatform Create(Context context, ILoggerFactory loggerFactory)
{
var btService = (BluetoothManager)context.GetSystemService(Context.BluetoothService)!;
Expand Down

0 comments on commit 652173a

Please sign in to comment.