Search code examples
loggingcastle-windsorserilog

Logger.InfoFormat not logging first parameter using serilog and castle winsdor log


I'm using Serilog to log my request and responses in console and ElasticSearch.

The code looks like this:

public class HttpLogDto
{
    public string Method { get; set; }
    public string Url { get; set; }
    public string QueryString { get; set; }
    public Dictionary<string, string[]> RequestHeaders { get; set; }
    public DateTime RequestedAt { get; set; }
    public string Request { get; set; }
    public Dictionary<string, string[]> ResponseHeaders { get; set; }
    public DateTime ResponsedAt { get; set; }
    public object Response { get; set; }
    public long ElapsedMilliseconds { get; set; }
}

public class RequestResponseLoggingMiddleware
{
    private readonly Castle.Core.Logging.ILogger _logger;
    public RequestResponseLoggingMiddleware(RequestDelegate next,Castle.Core.Logging.ILoggerFactory loggerFactory)
    {
        _next = next;
        _logger = loggerFactory.Create(typeof(RequestResponseLoggingMiddleware));
    }
    public async Task Invoke(HttpContext context)
    {
        HttpLogDto logDto = new logDto();
        //perform request and fill logDto

        _logger.InfoFormat("{\"Url\": {0},\"Method\": {1}, \"QueryString\": {2},\"RequestHeaders\": {3},\"RequestedAt\": {4} }", 
            new object[] {
                logDto.Url,
                logDto.Method,
                logDto.QueryString,
                logDto.RequestHeaders,
                logDto.RequestedAt});
    }
}

The problem is, it never fills the first parameter, and the output log is like this:

enter image description here


Solution

  • I removed those two curly brackets before and after the text, and this fixed the problem.