Open NicolasPrivatViseo opened 5 years ago
@TomPallister Any thoughts on that feature request ?
Did you read the docs?
In order to use custom logging you have to
develop your own IOcelotLogger
custom implementation, for example MyLogger
develop your own IOcelotLoggerFactory
custom implementation, for example MyLoggerFactory
https://github.com/ThreeMammals/Ocelot/blob/d6eefc899f9b2d37814335c7d0bccd5f7a830bd0/src/Ocelot/Logging/OcelotLoggerFactory.cs#L17-L21
new creation
public IOcelotLogger CreateLogger<T>()
{
var logger = _loggerFactory.CreateLogger<T>();
return new MyLogger(logger, _scopedDataRepository);
}
replace default OcelotLoggerFactory
class in DI container like this
https://github.com/ThreeMammals/Ocelot/blob/d6eefc899f9b2d37814335c7d0bccd5f7a830bd0/src/Ocelot/DependencyInjection/OcelotBuilder.cs#L94
and inject new MyLoggerFactory
class
Services.RemoveAll<IOcelotLoggerFactory>();
Services.TryAddSingleton<IOcelotLoggerFactory, MyLoggerFactory>();
Good luck!
@ggnaegi Your opinion regarding force writing HttpContext.Connection.RemoteIpAddress
into the logs by default OcelotLogger
implementation in Ocelot?
Currently we use RemoteIpAddress
in a limited number of Ocelot features: Search
And there is no unified logic to detect remote host...
New Feature
Add RemoteIpAddress in log message (IP of caller)
Motivation for New Feature
Current log message is
_logger.Log[Level]("requestId: {requestId}, previousRequestId: {previousRequestId}, message: {message}", requestId, previousRequestId, message);
New log message could be
_logger.Log[Level]("remoteIpAddress: {remoteIpAddress}, requestId: {requestId}, previousRequestId: {previousRequestId}, message: {message}",remoteIpAddress, requestId, previousRequestId, message);
The IP address is a security element that needs to be traced in some cases for security/analysis purposes.
Solutions tried
Forward IP in header or middleware injection is not suitable in this case because we need the information in the logs generated by Ocelot.
A have tried locally to add IP in log message (class AspDotNetLogger.cs), but the HttpContextAccessor is not accessible in the
_scopedDataRepository
variable, which is an instance of HttpDataRepository. I cannot get it without modification of IRequestScopedDataRepository that would have implications I am not aware of.