简体   繁体   中英

How to change OWIN request logged by IIS

With my custom OWIN middleware, I want to catch GET request to add a parameter to the query string.

I don't know if I'm correct, but actually I do it like this:

context.Request.QueryString = new QueryString(context.Request.QueryString.ToString() + "param=value")

But I also want that the IIS logging file entry for that request to be the updated request, not the original one.

Original log:

2015-07-22 09:32:35 ::1 GET /img.gif - 56782 - ::1 Mozilla/5.0+(Windows+NT+6.3;+WOW64;+Trident/7.0;+rv:11.0)+like+Gecko - 200 0 0 102891

Expected log:

2015-07-22 09:32:35 ::1 GET /img.gif param=value 56782 - ::1 Mozilla/5.0+(Windows+NT+6.3;+WOW64;+Trident/7.0;+rv:11.0)+like+Gecko - 200 0 0 102891

How can I do that?

For me the better option is create to own file log, not based on external logs (in this case IIS logs). For example you can use http://nlog-project.org/ and use it in your own OwinMiddleware.

public class GetMethodMiddleware : OwinMiddleware
{

    public GetMethodMiddleware(OwinMiddleware next) :
        base(next)
    { }

    public override async Task Invoke(IOwinContext context)
    {
        var logger = ObjectFactoryHost.ObjectFactory.GetObject<ILogger>();
        var requestMethod = (string)context.Request.Environment["owin.RequestMethod"];
        var requestQueryString = (string)context.Request.Environment["owin.RequestQueryString"];
        var requestPathString = (string)context.Request.Environment["owin.RequestPath"];
        logger.Info(string.Format("{0}:{1}:{2}", requestMethod, requestQueryString,requestPathString));

        await Next.Invoke(context);
    }
}

and use it in startup configuration

app.Use(typeof(GetMethodMiddleware));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM