Skip to content

Commit

Permalink
✨ Support Diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
catcherwong committed Feb 24, 2019
1 parent bf31532 commit f2b3f3a
Show file tree
Hide file tree
Showing 5 changed files with 669 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
namespace EasyCaching.Core.Diagnostics
{
using System.Diagnostics;

public static class EasyCachingDiagnosticListenerExtensions
{
/// <summary>
/// The name of the diagnostic listener.
/// </summary>
public const string DiagnosticListenerName = "EasyCachingDiagnosticListener";

public const string EasyCachingSetCache = nameof(WriteSetCache);
public const string EasyCachingExistsCache = nameof(WriteExistsCache);
public const string EasyCachingFlushCache = nameof(WriteFlushCache);
public const string EasyCachingRemoveCache = nameof(WriteRemoveCache);
public const string EasyCachingGetCache = nameof(WriteGetCache);
public const string EasyCachingGetCount = nameof(WriteGetCount);
public const string EasyCachingSetAll = nameof(WriteSetAll);

public static void WriteSetCache(this DiagnosticListener @this, SetCacheEventData eventData)
{
if (@this.IsEnabled(EasyCachingSetCache))
{
@this.Write(EasyCachingSetCache, eventData);
}
}

public static void WriteRemoveCache(this DiagnosticListener @this, RemoveCacheEventData eventData)
{
if (@this.IsEnabled(EasyCachingRemoveCache))
{
@this.Write(EasyCachingRemoveCache, eventData);
}
}

public static void WriteGetCache(this DiagnosticListener @this, GetCacheEventData eventData)
{
if (@this.IsEnabled(EasyCachingGetCache))
{
@this.Write(EasyCachingGetCache, eventData);
}
}

public static void WriteSetAll(this DiagnosticListener @this, SetAllEventData eventData)
{
if (@this.IsEnabled(EasyCachingSetAll))
{
@this.Write(EasyCachingSetAll, eventData);
}
}

public static void WriteGetCount(this DiagnosticListener @this, GetCountEventData eventData)
{
if (@this.IsEnabled(EasyCachingGetCount))
{
@this.Write(EasyCachingGetCount, eventData);
}
}

public static void WriteExistsCache(this DiagnosticListener @this, ExistsCacheEventData eventData)
{
if (@this.IsEnabled(EasyCachingExistsCache))
{
@this.Write(EasyCachingExistsCache, eventData);
}
}

public static void WriteFlushCache(this DiagnosticListener @this, EventData eventData)
{
if (@this.IsEnabled(EasyCachingFlushCache))
{
@this.Write(EasyCachingFlushCache, eventData);
}
}
}
}
101 changes: 101 additions & 0 deletions src/EasyCaching.Core/Diagnostics/EventData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
namespace EasyCaching.Core.Diagnostics
{
using System;

public class EventData
{
public EventData(string cacheType, string name, string operation)
{
this.CacheType = cacheType;
this.Name = name;
this.Operation = operation;
}

public string CacheType { get; set; }

public string Name { get; set; }

public string Operation { get; set; }
}

public class RemoveCacheEventData : EventData
{
public RemoveCacheEventData(string cacheType, string name, string operation, string[] cacheKeys)
: base(cacheType, name, operation)
{
this.CacheKeys = cacheKeys;
}

public string[] CacheKeys { get; set; }
}

public class GetCacheEventData : EventData
{
public GetCacheEventData(string cacheType, string name, string operation, string[] cacheKeys)
: base(cacheType, name, operation)
{
this.CacheKeys = cacheKeys;
}

public string[] CacheKeys { get; set; }
}

public class GetCountEventData : EventData
{
public GetCountEventData(string cacheType, string name, string operation, string prefix, long count)
: base(cacheType, name, operation)
{
this.Prefix = prefix;
this.Count = count;
}

public string Prefix { get; set; }

public long Count { get; set; }
}

public class SetCacheEventData : EventData
{
public SetCacheEventData(string cacheType, string name, string operation, string cacheKey, object cacheValue, TimeSpan expiration)
: base(cacheType, name, operation)
{
this.CacheKey = cacheKey;
this.CacheValue = cacheValue;
this.Expiration = expiration;
}

public string CacheKey { get; set; }

public object CacheValue { get; set; }

public TimeSpan Expiration { get; set; }
}

public class SetAllEventData : EventData
{
public SetAllEventData(string cacheType, string name, string operation, object values, TimeSpan expiration)
: base(cacheType, name, operation)
{
this.Values = values;
this.Expiration = expiration;
}

public object Values { get; set; }

public TimeSpan Expiration { get; set; }
}

public class ExistsCacheEventData : EventData
{
public ExistsCacheEventData(string cacheType, string name, string operation, string cacheKey, bool flag)
: base(cacheType, name, operation)
{
this.CacheKey = cacheKey;
this.Flag = flag;
}

public string CacheKey { get; set; }

public bool Flag { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/EasyCaching.Core/EasyCaching.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
<Folder Include="Interceptor\" />
<Folder Include="ProviderFactory\" />
<Folder Include="Stats\" />
<Folder Include="Diagnostics\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.1.1" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.5.1" />
</ItemGroup>
</Project>
Loading

0 comments on commit f2b3f3a

Please sign in to comment.