Skip to content

Commit

Permalink
fix logger creator
Browse files Browse the repository at this point in the history
  • Loading branch information
msmolka committed Dec 9, 2016
1 parent fceb949 commit d891d92
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 47 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ When you install the package, it should be added to your `package.json`. Alterna
```json
{
"dependencies" : {
"ZNetCS.AspNetCore.Logging.EntityFrameworkCore": "1.0.0"
"ZNetCS.AspNetCore.Logging.EntityFrameworkCore": "1.0.1"
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"projects": [ "src", "test"],
"projects": [ "src", "test" ],
"sdk": {
"version": "1.0.0-preview2-003131"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ public class EntityFrameworkLogger<TContext> : EntityFrameworkLogger<TContext, L
/// <param name="creator">
/// The creator used to create new instance of log.
/// </param>
public EntityFrameworkLogger(IServiceProvider serviceProvider, string name, Func<string, LogLevel, bool> filter, Func<int, int, string, string, Log> creator)
public EntityFrameworkLogger(
IServiceProvider serviceProvider,
string name,
Func<string, LogLevel, bool> filter,
Func<int, int, string, string, Log> creator = null)
: base(serviceProvider, name, filter, creator)
{
}
Expand Down Expand Up @@ -85,7 +89,11 @@ public class EntityFrameworkLogger<TContext, TLog> : EntityFrameworkLogger<TCont
/// <param name="creator">
/// The creator used to create new instance of log.
/// </param>
public EntityFrameworkLogger(IServiceProvider serviceProvider, string name, Func<string, LogLevel, bool> filter, Func<int, int, string, string, TLog> creator)
public EntityFrameworkLogger(
IServiceProvider serviceProvider,
string name,
Func<string, LogLevel, bool> filter,
Func<int, int, string, string, TLog> creator = null)
: base(serviceProvider, name, filter, creator)
{
}
Expand Down Expand Up @@ -152,13 +160,27 @@ public class EntityFrameworkLogger<TContext, TLog, TKey> : IEntityFrameworkLogge
/// <param name="creator">
/// The creator used to create new instance of log.
/// </param>
public EntityFrameworkLogger(IServiceProvider serviceProvider, string name, Func<string, LogLevel, bool> filter, Func<int, int, string, string, TLog> creator)
public EntityFrameworkLogger(
IServiceProvider serviceProvider,
string name,
Func<string, LogLevel, bool> filter,
Func<int, int, string, string, TLog> creator = null)
{
if (serviceProvider == null)
{
throw new ArgumentNullException(nameof(serviceProvider));
}

if (filter == null)
{
throw new ArgumentNullException(nameof(filter));
}

this.serviceProvider = serviceProvider;

this.name = name ?? string.Empty;
this.filter = filter;
this.creator = creator;
this.creator = creator ?? this.DefaultCreator;
}

#endregion
Expand Down Expand Up @@ -241,6 +263,34 @@ protected virtual void WriteMessage(string message, LogLevel logLevel, int event
}
}

/// <summary>
/// The default log creator method.
/// </summary>
/// <param name="logLevel">
/// The log level.
/// </param>
/// <param name="eventId">
/// The event id.
/// </param>
/// <param name="logName">
/// The log name.
/// </param>
/// <param name="message">
/// The message.
/// </param>
private TLog DefaultCreator(int logLevel, int eventId, string logName, string message)
{
var log = ActivatorUtilities.CreateInstance<TLog>(this.serviceProvider);

log.TimeStamp = DateTimeOffset.Now;
log.Level = logLevel;
log.EventId = eventId;
log.Name = logName.Length > 255 ? logName.Substring(0, 255) : logName;
log.Message = message;

return log;
}

#endregion

#region Nested Classes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public class EntityFrameworkLoggerProvider<TContext> : EntityFrameworkLoggerProv
/// <param name="creator">
/// The creator used to create new instance of log.
/// </param>
public EntityFrameworkLoggerProvider(IServiceProvider serviceProvider, Func<string, LogLevel, bool> filter, Func<int, int, string, string, Log> creator = null) : base(serviceProvider, filter, creator)
public EntityFrameworkLoggerProvider(IServiceProvider serviceProvider, Func<string, LogLevel, bool> filter, Func<int, int, string, string, Log> creator = null)
: base(serviceProvider, filter, creator)
{
}

Expand Down Expand Up @@ -78,7 +79,8 @@ public class EntityFrameworkLoggerProvider<TContext, TLog> : EntityFrameworkLogg
/// <param name="creator">
/// The creator used to create new instance of log.
/// </param>
public EntityFrameworkLoggerProvider(IServiceProvider serviceProvider, Func<string, LogLevel, bool> filter, Func<int, int, string, string, TLog> creator = null) : base(serviceProvider, filter, creator)
public EntityFrameworkLoggerProvider(IServiceProvider serviceProvider, Func<string, LogLevel, bool> filter, Func<int, int, string, string, TLog> creator = null)
: base(serviceProvider, filter, creator)
{
}

Expand Down Expand Up @@ -117,7 +119,8 @@ public class EntityFrameworkLoggerProvider<TContext, TLog, TLogger> : EntityFram
/// <param name="creator">
/// The creator used to create new instance of log.
/// </param>
public EntityFrameworkLoggerProvider(IServiceProvider serviceProvider, Func<string, LogLevel, bool> filter, Func<int, int, string, string, TLog> creator = null) : base(serviceProvider, filter, creator)
public EntityFrameworkLoggerProvider(IServiceProvider serviceProvider, Func<string, LogLevel, bool> filter, Func<int, int, string, string, TLog> creator = null)
: base(serviceProvider, filter, creator)
{
}

Expand Down Expand Up @@ -153,6 +156,11 @@ public class EntityFrameworkLoggerProvider<TContext, TLog, TLogger, TKey> : IEnt
/// </summary>
private readonly Func<int, int, string, string, TLog> creator;

/// <summary>
/// The object factory to create new logger used defined types.
/// </summary>
private readonly ObjectFactory factory;

/// <summary>
/// The function used to filter events based on the log level.
/// </summary>
Expand Down Expand Up @@ -191,13 +199,17 @@ public EntityFrameworkLoggerProvider(IServiceProvider serviceProvider, Func<stri
throw new ArgumentNullException(nameof(serviceProvider));
}

this.serviceProvider = serviceProvider;
this.filter = filter;

if (creator == null)
if (filter == null)
{
this.creator = this.DefaultCreator;
throw new ArgumentNullException(nameof(filter));
}

this.serviceProvider = serviceProvider;
this.filter = filter;
this.creator = creator;
this.factory = ActivatorUtilities.CreateFactory(
typeof(TLogger),
new[] { typeof(string), typeof(Func<string, LogLevel, bool>), typeof(Func<int, int, string, string, TLog>) });
}

#endregion
Expand All @@ -223,7 +235,7 @@ public void Dispose()
public virtual ILogger CreateLogger(string categoryName)
{
this.ThrowIfDisposed();
return ActivatorUtilities.CreateInstance<TLogger>(this.serviceProvider, categoryName, this.filter, this.creator);
return (ILogger)this.factory(this.serviceProvider, new object[] { categoryName, this.filter, this.creator });
}

#endregion
Expand Down Expand Up @@ -254,34 +266,6 @@ protected void ThrowIfDisposed()
}
}

/// <summary>
/// The default log creator method.
/// </summary>
/// <param name="logLevel">
/// The log level.
/// </param>
/// <param name="eventId">
/// The event id.
/// </param>
/// <param name="name">
/// The log name.
/// </param>
/// <param name="message">
/// The message.
/// </param>
private TLog DefaultCreator(int logLevel, int eventId, string name, string message)
{
var log = ActivatorUtilities.CreateInstance<TLog>(this.serviceProvider);

log.TimeStamp = DateTimeOffset.Now;
log.Level = logLevel;
log.EventId = eventId;
log.Name = name.Length > 255 ? name.Substring(0, 255) : name;
log.Message = message;

return log;
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.0.0",
"version": "1.0.1",
"packOptions": {
"owners": [ "Marcin Smółka" ],
"licenseUrl": "https://raw.githubusercontent.com/msmolka/ZNetCS.AspNetCore.Logging.EntityFrameworkCore/master/LICENSE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddEntityFrameworkInMemoryDatabase();

// Add framework services.
services.AddDbContext<ContextSimple>(options => options.UseInMemoryDatabase("SimpleLogDatabase"));
services.AddDbContext<ContextSimple>(options => options.UseInMemoryDatabase("SimpleLogCreatorDatabase"));
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class TestLoggerSimpleCreator : TestBase
/// <summary>
/// Initializes a new instance of the <see cref="TestLoggerSimpleCreator"/> class.
/// </summary>
public TestLoggerSimpleCreator() : base(0)
public TestLoggerSimpleCreator() : base(3)
{
}

Expand All @@ -47,7 +47,7 @@ public TestLoggerSimpleCreator() : base(0)
public async Task WriteSimpleCreatorLog()
{
var options = new DbContextOptionsBuilder<ContextSimple>()
.UseInMemoryDatabase("SimpleLogDatabase")
.UseInMemoryDatabase("SimpleLogCreatorDatabase")
.Options;

// Act
Expand Down

0 comments on commit d891d92

Please sign in to comment.