Skip to content

Commit

Permalink
Make Logger name and creator virtual property for easier overriding
Browse files Browse the repository at this point in the history
  • Loading branch information
msmolka committed Dec 21, 2016
1 parent d891d92 commit 6ad9e5f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 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.1"
"ZNetCS.AspNetCore.Logging.EntityFrameworkCore": "1.0.2"
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,11 @@ public class EntityFrameworkLogger<TContext, TLog, TKey> : IEntityFrameworkLogge
{
#region Fields

/// <summary>
/// The function used to create new model instance for a log.
/// </summary>
private readonly Func<int, int, string, string, TLog> creator;

/// <summary>
/// The function used to filter events based on the log level.
/// </summary>
private readonly Func<string, LogLevel, bool> filter;

/// <summary>
/// The name of the logger.
/// </summary>
private readonly string name;

/// <summary>
/// The service provider to resolve dependency.
/// </summary>
Expand Down Expand Up @@ -178,13 +168,28 @@ public EntityFrameworkLogger(

this.serviceProvider = serviceProvider;

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

this.Name = name ?? string.Empty;
this.Creator = creator ?? this.DefaultCreator;
}

#endregion

#region Properties

/// <summary>
/// Gets the function used to create new model instance for a log.
/// </summary>
protected virtual Func<int, int, string, string, TLog> Creator { get; }

/// <summary>
/// Gets the name of the logger.
/// </summary>
protected virtual string Name { get; }

#endregion

#region Implemented Interfaces

#region ILogger
Expand All @@ -198,7 +203,7 @@ public virtual IDisposable BeginScope<TState>(TState state)
/// <inheritdoc />
public virtual bool IsEnabled(LogLevel logLevel)
{
return (this.filter == null) || this.filter(this.name, logLevel);
return (this.filter == null) || this.filter(this.Name, logLevel);
}

/// <inheritdoc />
Expand Down Expand Up @@ -255,7 +260,7 @@ protected virtual void WriteMessage(string message, LogLevel logLevel, int event
using (var context = ActivatorUtilities.CreateInstance<TContext>(this.serviceProvider))
{
// create new log with resolving dependency injection
TLog log = this.creator((int)logLevel, eventId, this.name, message);
TLog log = this.Creator((int)logLevel, eventId, this.Name, message);

context.Set<TLog>().Add(log);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.0.1",
"version": "1.0.2",
"packOptions": {
"owners": [ "Marcin Smółka" ],
"licenseUrl": "https://raw.githubusercontent.com/msmolka/ZNetCS.AspNetCore.Logging.EntityFrameworkCore/master/LICENSE",
Expand Down

0 comments on commit 6ad9e5f

Please sign in to comment.