1
votes

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?

1
I don't think you can - and that's usually a good thing. Suppose your code got something wrong and erased information. How would you know what the nature of the request was that triggered that bug if you couldn't trust the IIS logs?Damien_The_Unbeliever

1 Answers

0
votes

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));