-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding back the logger methods with string as parameter, avoiding cal…
…ling the factory when plain string are used.
- Loading branch information
Showing
15 changed files
with
170 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,108 @@ | ||
using Microsoft.Extensions.Logging; | ||
using Ocelot.Infrastructure.RequestData; | ||
|
||
namespace Ocelot.Logging | ||
{ | ||
public class AspDotNetLogger : IOcelotLogger | ||
{ | ||
private readonly ILogger _logger; | ||
private readonly IRequestScopedDataRepository _scopedDataRepository; | ||
private readonly Func<string, Exception, string> _func; | ||
|
||
public AspDotNetLogger(ILogger logger, IRequestScopedDataRepository scopedDataRepository) | ||
{ | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
_scopedDataRepository = scopedDataRepository ?? throw new ArgumentNullException(nameof(scopedDataRepository)); | ||
_func = (state, exception) => exception == null ? state : $"{state}, exception: {exception}"; | ||
} | ||
|
||
public void LogTrace(Func<string> messageFactory) | ||
{ | ||
WriteLog(LogLevel.Trace, messageFactory); | ||
} | ||
|
||
public void LogDebug(Func<string> messageFactory) | ||
|
||
namespace Ocelot.Logging; | ||
|
||
public class AspDotNetLogger : IOcelotLogger | ||
{ | ||
private readonly ILogger _logger; | ||
private readonly IRequestScopedDataRepository _scopedDataRepository; | ||
private readonly Func<string, Exception, string> _func; | ||
|
||
public AspDotNetLogger(ILogger logger, IRequestScopedDataRepository scopedDataRepository) | ||
{ | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
_scopedDataRepository = scopedDataRepository; | ||
_func = (state, exception) => exception == null ? state : $"{state}, exception: {exception}"; | ||
} | ||
|
||
public void LogTrace(string message) | ||
{ | ||
LogTrace(() => message); | ||
} | ||
|
||
public void LogTrace(Func<string> messageFactory) | ||
{ | ||
WriteLog(LogLevel.Trace, messageFactory); | ||
} | ||
|
||
public void LogDebug(string message) | ||
{ | ||
LogDebug(() => message); | ||
} | ||
|
||
public void LogDebug(Func<string> messageFactory) | ||
{ | ||
WriteLog(LogLevel.Debug, messageFactory); | ||
} | ||
|
||
public void LogInformation(string message) | ||
{ | ||
LogInformation(() => message); | ||
} | ||
|
||
public void LogInformation(Func<string> messageFactory) | ||
{ | ||
WriteLog(LogLevel.Information, messageFactory); | ||
} | ||
|
||
public void LogWarning(string message) | ||
{ | ||
LogWarning(() => message); | ||
} | ||
|
||
public void LogWarning(Func<string> messageFactory) | ||
{ | ||
WriteLog(LogLevel.Warning, messageFactory); | ||
} | ||
|
||
public void LogError(string message, Exception exception) | ||
{ | ||
LogError(() => message, exception); | ||
} | ||
|
||
public void LogError(Func<string> messageFactory, Exception exception) | ||
{ | ||
WriteLog(LogLevel.Error, messageFactory, exception); | ||
} | ||
|
||
public void LogCritical(string message, Exception exception) | ||
{ | ||
LogCritical(() => message, exception); | ||
} | ||
|
||
public void LogCritical(Func<string> messageFactory, Exception exception) | ||
{ | ||
WriteLog(LogLevel.Critical, messageFactory, exception); | ||
} | ||
|
||
private string GetOcelotRequestId() | ||
{ | ||
var requestId = _scopedDataRepository.Get<string>("RequestId"); | ||
|
||
return requestId == null || requestId.IsError ? "no request id" : requestId.Data; | ||
} | ||
|
||
private string GetOcelotPreviousRequestId() | ||
{ | ||
var requestId = _scopedDataRepository.Get<string>("PreviousRequestId"); | ||
|
||
return requestId == null || requestId.IsError ? "no previous request id" : requestId.Data; | ||
} | ||
|
||
public void WriteLog(LogLevel logLevel, Func<string> messageFactory, Exception exception = null) | ||
{ | ||
if (!_logger.IsEnabled(logLevel)) | ||
{ | ||
WriteLog(LogLevel.Debug, messageFactory); | ||
} | ||
|
||
public void LogInformation(Func<string> messageFactory) | ||
{ | ||
WriteLog(LogLevel.Information, messageFactory); | ||
} | ||
|
||
public void LogWarning(Func<string> messageFactory) | ||
{ | ||
WriteLog(LogLevel.Warning, messageFactory); | ||
} | ||
|
||
public void LogError(Func<string> messageFactory, Exception exception) | ||
{ | ||
WriteLog(LogLevel.Error, messageFactory, exception); | ||
} | ||
|
||
public void LogCritical(Func<string> messageFactory, Exception exception) | ||
{ | ||
WriteLog(LogLevel.Critical, messageFactory, exception); | ||
} | ||
|
||
private string GetOcelotRequestId() | ||
{ | ||
var requestId = _scopedDataRepository.Get<string>("RequestId"); | ||
|
||
return requestId == null || requestId.IsError ? "no request id" : requestId.Data; | ||
} | ||
|
||
private string GetOcelotPreviousRequestId() | ||
{ | ||
var requestId = _scopedDataRepository.Get<string>("PreviousRequestId"); | ||
|
||
return requestId == null || requestId.IsError ? "no previous request id" : requestId.Data; | ||
} | ||
|
||
private void WriteLog(LogLevel logLevel, Func<string> messageFactory, Exception exception = null) | ||
{ | ||
if (!_logger.IsEnabled(logLevel)) | ||
{ | ||
return; | ||
} | ||
|
||
var requestId = GetOcelotRequestId(); | ||
var previousRequestId = GetOcelotPreviousRequestId(); | ||
|
||
var state = $"requestId: {requestId}, previousRequestId: {previousRequestId}, message: {messageFactory.Invoke()}"; | ||
|
||
_logger.Log(logLevel, default, state, exception, _func); | ||
} | ||
} | ||
return; | ||
} | ||
|
||
var requestId = GetOcelotRequestId(); | ||
var previousRequestId = GetOcelotPreviousRequestId(); | ||
|
||
var state = | ||
$"requestId: {requestId}, previousRequestId: {previousRequestId}, message: {messageFactory.Invoke()}"; | ||
|
||
_logger.Log(logLevel, default, state, exception, _func); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,25 @@ | ||
using System.Diagnostics; | ||
namespace Ocelot.Logging; | ||
|
||
namespace Ocelot.Logging | ||
/// <summary> | ||
/// Thin wrapper around the DotNet core logging framework, used to allow the scopedDataRepository to be injected giving access to the Ocelot RequestId. | ||
/// </summary> | ||
public interface IOcelotLogger | ||
{ | ||
/// <summary> | ||
/// Thin wrapper around the DotNet core logging framework, used to allow the scopedDataRepository to be injected giving access to the Ocelot RequestId. | ||
/// </summary> | ||
public interface IOcelotLogger | ||
{ | ||
void LogTrace(Func<string> messageFactory); | ||
|
||
void LogDebug(Func<string> messageFactory); | ||
void LogTrace(string message); | ||
void LogTrace(Func<string> messageFactory); | ||
|
||
void LogInformation(Func<string> messageFactory); | ||
void LogDebug(string message); | ||
void LogDebug(Func<string> messageFactory); | ||
|
||
void LogWarning(Func<string> messageFactory); | ||
void LogInformation(string message); | ||
void LogInformation(Func<string> messageFactory); | ||
|
||
void LogError(Func<string> messageFactory, Exception exception); | ||
void LogWarning(string message); | ||
void LogWarning(Func<string> messageFactory); | ||
|
||
void LogCritical(Func<string> messageFactory, Exception exception); | ||
} | ||
void LogError(string message, Exception exception); | ||
void LogError(Func<string> messageFactory, Exception exception); | ||
|
||
void LogCritical(string message, Exception exception); | ||
void LogCritical(Func<string> messageFactory, Exception exception); | ||
} |
Oops, something went wrong.