Open or create a text file every day on Application_Start and Append with requested web pages then close on Application_End

StackOverflow https://stackoverflow.com/questions/13628135

Frage

I have been tasked with running a simple audit that will record each and every page that has been requested by any user and store the data of the page and if access was granted or denied based on existing security trimming.

I am thinking along the lines of opening a text file on Application_Start as opposed to every active session attempting to write to a file that could be potentially opened by anyone at any given point. Yes I know the problem with this. Ideally I need a good approach or some help with how I am approaching my solution.

We have a custom class that checks if the current context has access to the page from their session. I want to utilise that class to pass that requested page details and the result of a successful/denied request. Trouble is I'm not greatly skilled and I have much to learn.

I am considering something like;

 if (File.Exists(@"\\MyServer\ICT\EFOSMS_AUDIT\" + DateTime.Today + ".txt"))
    {
        StreamReader read = new StreamReader(@"\\MyServer\ICT\EFOSMS_AUDIT\" + DateTime.Today);
    }
    else
    {
        File.OpenWrite(@"\\MyServer\ICT\EFOSMS_AUDIT\" + DateTime.Today);
    }

Just open the file and keep it open for the time the Application is running.

Then in said class, some existing code checks AD groups;

    CustomPrincipal principal = HttpContext.Current.User as CustomPrincipal;

                if (!principal.IsInAnyRoles(roles))
                {
                    Page.Response.Redirect("~/fos/AccessDenied.aspx");
//Write to file that is open at application level here??
                    return;
                }

I know in my head what it is that I am trying to achieve but dont fully understand if this is the best way to do it. I am not sure if there are any existing classes to make this any easier. I am working with StreamReader, StreamWriter, File.Exists, File.Open.

Can someone give a a nudge in the right direction.

thanks

War es hilfreich?

Lösung

What you are trying to achieve is quite similar to logging. There are several libraries that will allow you to write to text files, Log4Net, Nlog etc.

If you do not want to employ any of these tools, then perhaps you should consider making the logging functions and reference to the open file static. This way they will share information across page calls.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top