Skip to content

Commit

Permalink
Merge pull request #6 from daenetCorporation/netcore20
Browse files Browse the repository at this point in the history
Better .net core 2.0 compatibility
  • Loading branch information
hvetter-de authored Feb 13, 2018
2 parents 4636ccd + 5533dd4 commit 3260e35
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 99 deletions.
72 changes: 22 additions & 50 deletions Daenet.Common.Logging.EventHub/EventHubLogScopeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,77 +12,49 @@ namespace Daenet.Common.Logging.EventHub
/// </summary>
internal class EventHubLogScopeManager
{
internal static readonly AsyncLocal<List<DisposableScope>> m_AsyncSopes = new AsyncLocal<List<DisposableScope>>();
private object m_State;
private readonly string _name;
private readonly object _state;

internal EventHubLogScopeManager(object state)
internal EventHubLogScopeManager(string name, object state)
{
m_AsyncSopes.Value = new List<DisposableScope>();
m_State = state;
_name = name;
_state = state;
}

public string Current
public EventHubLogScopeManager Parent { get; private set; }

private static AsyncLocal<EventHubLogScopeManager> _value = new AsyncLocal<EventHubLogScopeManager>();
public static EventHubLogScopeManager Current
{
set
{
_value.Value = value;
}
get
{
if (m_AsyncSopes.Value == null)
m_AsyncSopes.Value = new List<DisposableScope>();

StringBuilder sb = new StringBuilder();
foreach (var item in m_AsyncSopes.Value)
{
sb.Append($"/{item}");
}

return sb.ToString();
return _value.Value;
}
}

public IDisposable Push(object state)
public static IDisposable Push(string name, object state)
{
lock ("scope")
{
var newScope = new DisposableScope(state.ToString(), this);
var temp = Current;
Current = new EventHubLogScopeManager(name, state);
Current.Parent = temp;

m_AsyncSopes.Value.Add(newScope);

return newScope;
}
return new DisposableScope();
}

public override string ToString()
{
return m_State?.ToString();
return _state?.ToString();
}

internal class DisposableScope : IDisposable
private class DisposableScope : IDisposable
{
private EventHubLogScopeManager m_ScopeMgr;
private string m_ScopeName;

public DisposableScope(string scopeName, EventHubLogScopeManager scopeMgr)
{
m_ScopeName = scopeName;
m_ScopeMgr = scopeMgr;
}

public void Dispose()
{
lock ("scope")
{
var me = m_AsyncSopes.Value.FirstOrDefault(s => s == this);
if (me == null)
{
throw new InvalidOperationException("This should never happen!");
}

m_AsyncSopes.Value.Remove(me);
}
}

public override string ToString()
{
return m_ScopeName;
Current = Current.Parent;
}
}
}
Expand Down
37 changes: 27 additions & 10 deletions Daenet.Common.Logging.EventHub/EventHubLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class EventHubLogger : ILogger
private string m_CategoryName;

private EventHubClient m_EventHubClient;
private EventHubLogScopeManager m_ScopeManager;

/// <summary>
/// List of key value pairs, which will be logged with every message.
Expand Down Expand Up @@ -86,15 +85,9 @@ public IDisposable BeginScope<TState>(TState state)
throw new ArgumentNullException(nameof(state));
}

if (m_ScopeManager == null)
{
m_ScopeManager = new EventHubLogScopeManager(state);
}

var scope = m_ScopeManager.Push(state);

return scope;
return EventHubLogScopeManager.Push("EventHubLogger", state);
}


public bool IsEnabled(LogLevel logLevel)
{
Expand All @@ -119,7 +112,7 @@ private EventData defaultEventFormatter(LogLevel logLevel, EventId eventId, obje
var data = (System.Collections.Generic.IDictionary<String, Object>)expando;

data.Add("Name", m_CategoryName);
data.Add("Scope", m_ScopeManager == null ? null : m_ScopeManager.Current);
data.Add("Scope", getScopeInformation());
data.Add("EventId", eventId.ToString());
data.Add("Message", state.ToString());
data.Add("Level", logLevel);
Expand Down Expand Up @@ -158,5 +151,29 @@ private EventData defaultEventFormatter(LogLevel logLevel, EventId eventId, obje
}

#endregion

private string getScopeInformation()
{
StringBuilder builder = new StringBuilder();
var current = EventHubLogScopeManager.Current;
string scopeLog = string.Empty;
var length = builder.Length;

while (current != null)
{
if (length == builder.Length)
{
scopeLog = $"=> {current}";
}
else
{
scopeLog = $"=> {current} ";
}

builder.Insert(length, scopeLog);
current = current.Parent;
}
return builder.ToString();
}
}
}
11 changes: 10 additions & 1 deletion Daenet.Common.Logging.EventHub/EventHubLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
using System.Text;
using Microsoft.Azure.EventHubs;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Daenet.Common.Logging.EventHub
{
[ProviderAlias("EventHub")]
public class EventHubLoggerProvider : ILoggerProvider
{
private readonly ConcurrentDictionary<string, EventHubLogger> m_Loggers = new ConcurrentDictionary<string, EventHubLogger>();
Expand All @@ -26,10 +28,17 @@ public EventHubLoggerProvider(IEventHubLoggerSettings settings, Func<string, Log
{
this.m_EventDataFormatter = eventDataFormatter;
this.m_AdditionalValues = additionalValues;
this.m_Filter = filter;
if (filter == null)
m_Filter = ((category, logLevel) => true);
else
m_Filter = filter;
this.m_Settings = settings;
}

public EventHubLoggerProvider(IOptions<EventHubLoggerSettings> settings) : this(settings.Value)
{
}

public ILogger CreateLogger(string categoryName)
{
return m_Loggers.GetOrAdd(categoryName, createLoggerImplementation);
Expand Down
92 changes: 64 additions & 28 deletions Daenet.Common.Logging.EventHub/EventHubLoggerProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,81 @@ public static class EventHubLoggerProviderExtensions
{
#region Public Methods


/// <summary>
/// Adds a console logger named 'Console' to the factory.
/// </summary>
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
public static ILoggingBuilder AddEventHub(this ILoggingBuilder builder)
{
builder.Services.AddSingleton<ILoggerProvider, EventHubLoggerProvider>();
return builder;
}

/// <summary>
/// Adds a sql logger named 'SqlServerLogger' to the factory.
/// </summary>
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
/// <param name="configure"></param>
public static ILoggingBuilder AddEventHub(this ILoggingBuilder builder, Action<EventHubLoggerSettings> configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}

builder.AddEventHub();
builder.Services.Configure(configure);

return builder;
}

/// <summary>
/// Adds a Event HubLogger to the Logger factory.
/// </summary>
/// <param name="loggerFactory">The Logger factory instance.</param>
/// <param name="config">The .NET Core Configuration for the logger section.</param>
/// <returns></returns>
public static ILoggerFactory AddEventHub(this ILoggerFactory loggerFactory, IConfiguration config)
{
loggerFactory.AddProvider(new EventHubLoggerProvider(config.GetEventHubLoggerSettings(), null));
return loggerFactory;
}




public static IEventHubLoggerSettings GetEventHubLoggerSettings(this IConfiguration config)
{
EventHubLoggerSettings settings = new EventHubLoggerSettings();

settings.IncludeScopes = config.GetValue<bool>("IncludeScopes");
config.GetSection("Switches").Bind(settings.Switches);
// config.GetSection("Switches").Bind(settings.Switches);

settings.ConnectionString = config.GetSection("EventHub").GetValue<string>("ConnectionString");
settings.IncludeExceptionStackTrace = config.GetSection("EventHub").GetValue<bool>("IncludeExceptionStackTrace");
settings.RetryPolicy = getRetryPolicy(config.GetSection("EventHub").GetValue<int>("RetryPolicy"));
settings.ConnectionString = config.GetSection("EventHubSetting").GetValue<string>("ConnectionString");
settings.IncludeExceptionStackTrace = config.GetSection("EventHubSetting").GetValue<bool>("IncludeExceptionStackTrace");
settings.RetryPolicy = getRetryPolicy(config.GetSection("EventHubSetting").GetValue<int>("RetryPolicy"));

return settings;
}

/// <summary>
/// Set settings from configuration.
/// </summary>
/// <param name="config">Configuration for SQL Server Logging.</param>
/// <returns></returns>
public static void SetEventHubLoggerSettings(this EventHubLoggerSettings settings, IConfiguration config)
{
if (settings == null)
settings = new EventHubLoggerSettings();

settings.IncludeScopes = config.GetValue<bool>("IncludeScopes");
// config.GetSection("Switches").Bind(settings.Switches);

settings.ConnectionString = config.GetSection("EventHubSetting").GetValue<string>("ConnectionString");
settings.IncludeExceptionStackTrace = config.GetSection("EventHubSetting").GetValue<bool>("IncludeExceptionStackTrace");
settings.RetryPolicy = getRetryPolicy(config.GetSection("EventHubSetting").GetValue<int>("RetryPolicy"));
}


/// <summary>
/// Add EventHub with no filter
Expand Down Expand Up @@ -101,7 +160,6 @@ public static ILoggingBuilder AddEventHub(this ILoggingBuilder loggingBuilder,
/// <param name="loggingBuilder"></param>
/// <param name="settings"></param>
/// <returns></returns>

public static ILoggingBuilder AddEventHub(this ILoggingBuilder loggingBuilder, IEventHubLoggerSettings settings)
{
if (settings == null)
Expand All @@ -114,28 +172,6 @@ public static ILoggingBuilder AddEventHub(this ILoggingBuilder loggingBuilder, I
return loggingBuilder;
}



/// <summary>
/// Adds the logger to the factory.
/// </summary>
/// <param name="loggingBuilder"></param>
/// <param name="configure"></param>
/// <returns></returns>

public static ILoggingBuilder AddEventHub(this ILoggingBuilder loggingBuilder, Action<IEventHubLoggerSettings> configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}

loggingBuilder.Services.AddSingleton<ILoggerProvider, EventHubLoggerProvider>();

return loggingBuilder;
}



/// <summary>
/// Creates the default instance of EventHub logger provider, when no configuration is specified.
Expand Down
Loading

0 comments on commit 3260e35

Please sign in to comment.