Log file is not being written to from an HttpHandler

旧时模样 提交于 2019-11-28 08:46:01

问题


I want to capture all the requests going to *.jpg files on my server. To do so, I have created an HttpHandler whose code is as follows:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.Globalization;

namespace MyHandler
{
    public class NewHandler : IHttpHandler
    {
        public NewHandler()
        {}

        public void ProcessRequest(System.Web.HttpContext ctx)
        {
            HttpRequest req = ctx.Request;
            string path = req.PhysicalPath;
            string extension = null;

            string contentType = null;           
            extension = Path.GetExtension(path).ToLower();

            switch (extension)
            {
                case ".gif":
                    contentType = "image/gif";
                    break;
                case ".jpg":
                    contentType = "image/jpeg";
                    break;
                case ".png":
                    contentType = "image/png";
                    break;
                default:
                    throw new NotSupportedException("Unrecognized image type.");
            } 

            if (!File.Exists(path))
            {
                ctx.Response.Status = "Image not found";
                ctx.Response.StatusCode = 404;
            }
            else
            {
                ctx.Response.Write("The page request is " + ctx.Request.RawUrl.ToString());
                StreamWriter sw = new StreamWriter(@"C:\requestLog.txt", true);
                sw.WriteLine("Page requested at " + DateTime.Now.ToString() 
                               + ctx.Request.RawUrl); sw.Close();

                ctx.Response.StatusCode = 200;
                ctx.Response.ContentType = contentType;
                ctx.Response.WriteFile(path);
            }
        }

        public bool IsReusable{get {return true;}}
    }
}

After compiling this and adding it to my web application's Bin directory as a deference, I added the following in my web.config file:

<system.web>
    <httpHandlers>
      <add verb="*" path="*.jpg" type="MyHandler.NewHandler,MyHandler"/>
    </httpHandlers>
</system.web>

Then I also modified the IIS settings Home Directory -> Application Configuration and added aspnet_isapi.dll for the .jpg extension.

In the Handler, I have tried to write some stuff in the log file which I am creating in the C drive, but it is not writing to the log file and I am unable to find the bug.


回答1:


<httpHandlers>
   <add verb="" path=".jpg" type="MyHandler.NewHandler,MyHandler"/>
</httpHandlers> 

Is this just a layout problem of stackoverflow or did you omit the asterisk (*):

<httpHandlers>
   <add verb="*" path="*.jpg" type="MyHandler.NewHandler,MyHandler"/>
</httpHandlers> 

If it's not missing asterisk, I would try to see the Network tab in Firebug on Firefox (or use Fiddler - a http debugging proxy). What HTTP response code do you get from the call?




回答2:


Does the user your application is running under have access to C:\? That's usually not the case; allowing an application context access to the root directory presents a big security risk.

Instead create a specific directory, for example c:\logs and give the ASP.NET application pool's user account full rights to that directory alone.




回答3:


Check your permissions.

You didn't hear this from me, but give "Everyone" full control to your C: and then run your test and see if the log file is created. (Right mouse click on C:, Properties, Security tab, etc...)

Don't forget to remove the permission when done.

Giving "Everyone" full control is just testing whether you're running into a permissions issue. If true, than you can fix it properly.

Don't forget to remove the permission when done.



来源:https://stackoverflow.com/questions/385945/log-file-is-not-being-written-to-from-an-httphandler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!