Skip to content

Commit

Permalink
Moved event resolver disposing to event resolver base
Browse files Browse the repository at this point in the history
  • Loading branch information
jschick04 committed Oct 24, 2024
1 parent 2ec9a26 commit 0eca2a4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public partial class EventProviderDatabaseEventResolver : EventResolverBase, IEv

private readonly SemaphoreSlim _databaseAccessSemaphore = new(1);

private bool _disposedValue;

public EventProviderDatabaseEventResolver(IDatabaseCollectionProvider dbCollection) : this(dbCollection, (s, log) => { }) { }

public EventProviderDatabaseEventResolver(IDatabaseCollectionProvider dbCollection, Action<string, LogLevel> tracer) : base(tracer)
Expand Down Expand Up @@ -166,32 +164,6 @@ public void ResolveProviderDetails(EventRecord eventRecord, string owningLogName
}
}

protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
foreach (var context in _dbContexts)
{
context.Dispose();
}
}

providerDetails.Clear();

_disposedValue = true;

tracer($"{nameof(EventProviderDatabaseEventResolver)} Disposed at:\n{Environment.StackTrace}", LogLevel.Information);
}
}

public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

[GeneratedRegex("^(.+) (\\S+)$")]
private static partial Regex SplitProductAndVersionRegex();
}
26 changes: 24 additions & 2 deletions src/EventLogExpert.Eventing/EventResolvers/EventResolverBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace EventLogExpert.Eventing.EventResolvers;

public partial class EventResolverBase
public partial class EventResolverBase : IDisposable
{
/// <summary>
/// The mappings from the outType attribute in the EventModel XML template to determine if it should be displayed
Expand All @@ -29,7 +29,6 @@ public partial class EventResolverBase
];

private static readonly ConcurrentDictionary<string, string[]> FormattedPropertiesCache = [];
private static readonly StringCache XmlCache = new();

/// <summary>
/// These are already defined in System.Diagnostics.Eventing.Reader.StandardEventKeywords. However, the names
Expand All @@ -47,16 +46,26 @@ public partial class EventResolverBase
{ 0x80000000000000, "Classic" }
};

private static readonly StringCache XmlCache = new();

protected readonly ConcurrentDictionary<string, ProviderDetails?> providerDetails = new();
protected readonly Action<string, LogLevel> tracer;

protected bool disposed;

private readonly Regex _sectionsToReplace = WildcardWithNumberRegex();

protected EventResolverBase(Action<string, LogLevel> tracer)
{
this.tracer = tracer ?? throw new ArgumentNullException(nameof(tracer));
}

public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

public IEnumerable<string> GetKeywordsFromBitmask(EventRecord eventRecord)
{
if (eventRecord.Keywords is null or 0) { return []; }
Expand Down Expand Up @@ -159,6 +168,19 @@ public string ResolveTaskName(EventRecord eventRecord)
return taskName.TrimEnd('\0');
}

protected virtual void Dispose(bool disposing)
{
if (disposed) { return; }

if (disposing)
{
FormattedPropertiesCache.Clear();
providerDetails.Clear();
}

disposed = true;
}

protected string FormatDescription(
List<string> properties,
string? descriptionTemplate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,8 @@ namespace EventLogExpert.Eventing.EventResolvers;
/// </summary>
public class LocalProviderEventResolver(Action<string, LogLevel> tracer) : EventResolverBase(tracer), IEventResolver
{
private bool _disposedValue;

public LocalProviderEventResolver() : this((s, log) => Debug.WriteLine(s)) { }

public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

public void ResolveProviderDetails(EventRecord eventRecord, string owningLogName)
{
if (providerDetails.TryGetValue(eventRecord.ProviderName, out var details))
Expand All @@ -34,14 +26,4 @@ public void ResolveProviderDetails(EventRecord eventRecord, string owningLogName
details = new EventMessageProvider(eventRecord.ProviderName, tracer).LoadProviderDetails();
providerDetails.TryAdd(eventRecord.ProviderName, details);
}

protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
providerDetails.Clear();

_disposedValue = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace EventLogExpert.Eventing.EventResolvers;

/// <summary>This IEventResolver uses event databases if any are available, and falls back to local providers if not.</summary>
public class VersatileEventResolver : IEventResolver, IDisposable
public class VersatileEventResolver : IEventResolver
{
private readonly EventProviderDatabaseEventResolver _databaseResolver;
private readonly LocalProviderEventResolver _localResolver;
Expand Down Expand Up @@ -59,14 +59,13 @@ public string ResolveTaskName(EventRecord eventRecord) =>

protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
_databaseResolver.Dispose();
}
if (_disposedValue) { return; }

_disposedValue = true;
if (disposing)
{
_databaseResolver.Dispose();
}

_disposedValue = true;
}
}

0 comments on commit 0eca2a4

Please sign in to comment.