Skip to content

Commit

Permalink
remove logger dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Jan 6, 2024
1 parent aba9ec1 commit 88bc5bd
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 38 deletions.
2 changes: 1 addition & 1 deletion sandbox/ConsoleApp1/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
x.SetMinimumLevel(LogLevel.Trace);
x.AddZLoggerConsole();
});
ObservableSystem.Logger = factory.CreateLogger<ObservableSystem>();
// ObservableSystem.Logger = factory.CreateLogger<ObservableSystem>();
var logger = factory.CreateLogger<Program>();


Expand Down
34 changes: 4 additions & 30 deletions src/R3/ObservableSystem.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using static Microsoft.Extensions.Logging.LogLevel;

namespace R3;
namespace R3;

public class ObservableSystem
{
public static ILogger<ObservableSystem> Logger { get; set; } = NullLogger<ObservableSystem>.Instance;

public static TimeProvider DefaultTimeProvider { get; set; } = TimeProvider.System;
public static FrameProvider DefaultFrameProvider { get; set; } = new NotSupportedFrameProvider();

static Action<Exception> unhandledException = WriteLog;
static Action<Exception> unhandledException = DefaultUnhandledExceptionHandler;

// Prevent +=, use Set and Get method.
public static void RegisterUnhandledExceptionHandler(Action<Exception> unhandledExceptionHandler)
Expand All @@ -24,16 +18,9 @@ public static Action<Exception> GetUnhandledExceptionHandler()
return unhandledException;
}

static void WriteLog(Exception exception)
static void DefaultUnhandledExceptionHandler(Exception exception)
{
if (Logger == NullLogger<ObservableSystem>.Instance)
{
Console.WriteLine("R3 UnhandleException: " + exception.ToString());
}
else
{
Logger.UnhandledException(exception);
}
Console.WriteLine("R3 UnhandleException: " + exception.ToString());
}
}

Expand All @@ -49,16 +36,3 @@ public override void Register(IFrameRunnerWorkItem callback)
throw new NotSupportedException("ObservableSystem.DefaultFrameProvider is not set. Please set ObservableSystem.DefaultFrameProvider to a valid FrameProvider(ThreadSleepFrameProvider, etc...).");
}
}

internal static partial class SystemLoggerExtensions
{
[LoggerMessage(Trace, "Add subscription tracking TrackingId: {TrackingId}.")]
public static partial void AddTracking(this ILogger<ObservableSystem> logger, int trackingId);

[LoggerMessage(Trace, "Remove subscription TrackingId: {TrackingId}.")]
public static partial void RemoveTracking(this ILogger<ObservableSystem> logger, int trackingId);

[LoggerMessage(Error, "R3 EventSystem received unhandled exception.")]
public static partial void UnhandledException(this ILogger<ObservableSystem> logger, Exception exception);

}
8 changes: 7 additions & 1 deletion src/R3/R3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="PolySharp" Version="1.14.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -31,9 +30,16 @@
<PackageReference Include="Microsoft.Bcl.TimeProvider" Version="8.0.0" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework) == 'netstandard2.0'">
<PackageReference Include="System.Buffers" Version="4.5.1" />
<PackageReference Include="System.Memory" Version="4.5.5" />

</ItemGroup>

<ItemGroup Condition="$(TargetFramework) == 'netstandard2.0' Or $(TargetFramework) == 'netstandard2.1'">
<PackageReference Include="Microsoft.Bcl.TimeProvider" Version="8.0.0" />
<PackageReference Include="System.Threading.Channels" Version="8.0.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 1 addition & 6 deletions src/R3/SubscriptionTracker.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Microsoft.Extensions.Logging;
using R3.Internal;
using System.Diagnostics;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;

namespace R3;
Expand Down Expand Up @@ -65,7 +62,6 @@ internal static bool TryTrackActiveSubscriptionCore(IDisposable subscription, in

var id = Interlocked.Increment(ref trackingIdCounter);
trackableDisposable = new TrackableDisposable(subscription, id);
ObservableSystem.Logger.AddTracking(id);
tracking.TryAdd(trackableDisposable, new TrackingState(id, typeName, DateTime.Now, stackTrace)); // use local now.

return true;
Expand Down Expand Up @@ -167,7 +163,6 @@ public void Dispose()
var field = Interlocked.CompareExchange(ref disposed, 1, 0);
if (field == 0)
{
ObservableSystem.Logger.RemoveTracking(trackingId);
SubscriptionTracker.RemoveTracking(this);
}

Expand Down

0 comments on commit 88bc5bd

Please sign in to comment.