Serilog Enricher - TryGetGetCurrentRequest getting DTO

I’m trying to use a Serilog enricher to see if the DTO and/or type which has an attribute but the DTO always comes back null. Is there a way to get this? I’ve tried adding to the Items collection in the exception handler but it always empty when it gets to the enricher.

     Log.Logger = new LoggerConfiguration()
     .MinimumLevel.Is(AppSettings.Get<LogEventLevel>("logLevel", LogEventLevel.Error))
     .Enrich.FromLogContext()
     .Enrich.WithExceptionDetails()
      .Enrich.WithDynamicProperty("SendAlert", () =>
      {
          var req = HostContext.TryGetCurrentRequest();
          if (req != null && req.GetType().HasAttribute<AuthenticateAttribute>())  // req.Dto is null
          {
              var a = req.GetType().FirstAttribute<LogAlertAttribute>();
              if (a.SendAlert)
                  return "true";
          }
          return null;
      }, minimumLevel: LogEventLevel.Error);

  this.ServiceExceptionHandlers.Add((httpReq, request, exception) =>
        {
            //log your exceptions here
          
                Log.Error(exception, "ServiceStack exception handler");

            return null; //continue with default Error Handling

            //or return your own custom response
            //return DtoUtils.CreateErrorResponse(request, exception);
        });

The IRequest that’s retrieved statically doesn’t contain the Request DTO but you should be able to resolve it with something like:

var restPath = RestHandler.FindMatchingRestPath(req, out var _);
var dtoType = restPath.RequestType;
1 Like

Thank you. The full code for the enricher neded up being:

.Enrich.WithDynamicProperty("SendAlert", () =>
      {
         var req = HostContext.TryGetCurrentRequest();
          if (req == null) return null;
          var restPath = RestHandler.FindMatchingRestPath(req.Verb, req.PathInfo, out var _);
          var dtoType = restPath.RequestType;
          if (dtoType!=null && dtoType.HasAttribute<SendAlertAttribute>())
          {
              return "true";
          }
          return null;
      }, minimumLevel: LogEventLevel.Error)