Microsoft Owin Logging - Web Api 2 - How do I create the logger?
I'd recommend writing your middleware so that you can handle the logging outside of the controller:
public class LoggingMiddleware : OwinMiddleware
{
public LoggingMiddleware(OwinMiddleware next)
: base(next)
{
}
public override async Task Invoke(IOwinContext context)
{
//handle request logging
await Next.Invoke(context);
//handle response logging
}
}
Then in Startup class:
public class Startup
{
// ReSharper disable once UnusedMember.Global
public void Configuration(IAppBuilder appBuilder)
{
HttpConfiguration config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
appBuilder.Use<LoggingMiddleware>();
appBuilder.UseWebApi(config);
}
}
The request would then come in, hit the request logging code in the LoggingMiddleware, hit the controller code and then response would be logged on the LoggingMiddleware on the way back.
However, if all you are wanting to do is send an object through from middleware to the controller you can use context.Set("loggingObject", loggingObject);
in the middleware and then
var loggingObject = Request.GetOwinContext().Get<LoggerClass>("loggingObject");
in the controller.
Related videos on Youtube
Daniel
Updated on October 06, 2022Comments
-
Daniel over 1 year
I am trying to add logging to my app using Web Api 2 and Owin, so I started using Microsoft Owin Logging, which requires an
ILogger
andILoggerFactory
, that has been implemented and it works great when I need to log anything inside the STARTUP method or any of the Owin Middleware components.For example, when I am in the
Startup
method I can create the logger using:public void Configuration(IAppBuilder app) { // Creates configuration var configuration = new HttpConfiguration(); // Configure WebApi Settings WebApiConfig.Register(configuration); app.SetLoggerFactory(new OwinLog4NetLoggerFactory("Default")); var logger = app.CreateLogger<Startup>(); logger.WriteInformation("test log"); // Enabled WebApi in OWIN app.UseWebApi(configuration); }
Where "OwinLog4NetLoggerFactory" is my custom ILoggerFactory implementation.
So far, so good... but... How can I create the logger when I am in the actual web api action method?... I tried accessing the
Request.GetOwinEnvironment()
and the logger factory is not in the dictionary.For example:
public class AccountController : ApiController { public int Get(int id) { // Create logger here return id + 1; } }
I know I can create a static class with a reference to the Logger Factory or even Injection to add the logger to the api controller, but that seems too complicated for something that should be already there.
Any ideas would be appreciated.
-
Daniel over 8 yearsThank you so much, unfortunately I do not need to log each request/response (FYI, in the Owin world, you normally do that thru a Middleware). I need to create the logger because I need to log "some debug or warning messages" in some actions. Thank you again for your response.
-
Daniel almost 7 yearsThank you... I ended up using Log4Net directly in each controller class, but the idea was to use the build-in logging abstraction (not "another" package). Thanks!
-
Daniel almost 7 yearsThank you Sam, but that only lets me login BEFORE and AFTER the call to the Controller... not INSIDE the controller. I ended up using Log4Net inside each controller/class.... As recommended by Trevor, I could use a logging abstraction, so I do not have to create a direct dependency to Log4Net, but I wanted to use the internal logging abstraction provided by Microsoft Owin Logging. Thanks.