Reputation: 33
I want to use Serilog message templates with Loggly.
I have configured Loggly with Serilog into my application.
var logglySettings = new LogglySettings();
configuration.GetSection("Serilog:Loggly").Bind(logglySettings);
this.SetupLogglyConfiguration(logglySettings);
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
But I am not able to send multiple properties to loggly.com
For example I want to use:
public void Error<T, T1>(string message, T property0, T1 property1)
{
Log.Error(message, property0, property1);
}
I am able to send multiple properties as
return this.logger.Error("Could not save data.", new { Exception = ex, RequestData = requestData });
But this does not fit into Serilog pattern.
I wanted to use Serilog logging pattern as:
public void Error<T, T1>(string message, T property0, T1 property1)
Upvotes: 2
Views: 6898
Reputation: 1286
You need to provide a message template that includes all of the properties you want to log rather than just the message itself, e.g.
Log.Error("Error while saving {RequestData}: {Exception}", requestData, exception);
Since there's an explicit overload of Log.Error
that accepts an exception argument, I'd recommend using that instead, as this will enable cleaner presentation and more deep destructuring (e.g., via Demystify). You can also force deep-destructuring the request data using the @
template character if you want more than just its ToString()
representation, e.g.
Log.Error(exception, "Failed to save {@RequestData}", requestData)
Alternatively, if you specifically don't want to include the request data in the message itself you can add it as context information, e.g.
Log.ForContext("RequestData", requestData, destructureObjects: true)
.Error(exception, "Could not save data.")
Upvotes: 4