Currently I'm trying to figure out the common practice for storing message templates for logging (Serilog) so that they are easily accessible, specifically from my controllers.
This is how I currently store them, which would mean a ton of code duplication the second I need another controller.
[Route("api/[controller]")]
[ApiController]
public class TmpController : ControllerBase
{
#region Private Fields
private const string InvalidModelMessageTemplate = "Model is null or invalid: {Model}";
private const string NotFoundMessageTemplate = "Could not find {Entity} with id: {Id}";
private const string UnhandledExceptionMessageTemplate = "Unexpected exception in {Controller}: {Message}";
catch (Exception ex)
{
_logger.LogError(UnhandledExceptionMessageTemplate, nameof(TmpController), ex.Message);
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
}
The easiest way would be a static class, but I've got a feeling that's hacky and there are better ways to do this. Some built in functionality of Serilog, maybe? Base class for my controllers?
There are plenty of ways to do this, but none I've thought up so far feel very clean to me. Any common approaches?