title: Logging from inside a Customized Bad Request Response
body: <p>I'm capturing model validation errors using the code below and outputting a custom 400 response from the CustomProblemDetails object which works great. My question is, I want to log from within the CustomProblemDetails object but don't see how I can use DI. I've passed in context which gives me access to the services but is this the way to go? If so it appears I can only get access to the ILoggerFactory how do I log using ILoggerFactory?</p>
  
  <pre class="lang-cs prettyprint-override"><code>services.AddMvc()
      .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
      .ConfigureApiBehaviorOptions(options =&gt;
      {
          options.InvalidModelStateResponseFactory = context =&gt;
          {
              var problemDetails = new CustomProblemDetails(context)
              {
                  Type = "https://contoso.com/probs/modelvalidation",
                  Title = "One or more model validation errors occurred.",
                  Status = StatusCodes.Status400BadRequest,
                  Detail = "See the errors property for details.",
                  Instance = context.HttpContext.Request.Path
              };
  
              return new BadRequestObjectResult(problemDetails)
              {
                  ContentTypes = { "application/problem+json" }
              };
          };
      });
  </code></pre>