500pts: Advice with a Log class i have created, use static ? lock necessary?

Hi there,

I wonder if anyone can help i have created some webservices and in there is a separate class called "LOG" which basically writes to a text file when an error occurs...

I have created it as static methods so i don't need a reference but then of course i could always create a variable in global.asax and create a reference to it...

Just not too sure which is better,

also i am a little worried that more than one session accesses the log at the same time, is this something i can use lock or something for??

my log class as 3 methods, Open, writeError, and Close..... how would i implement a lock... before callign OPEN do i use lock (className)  and then after Close somehow unlock it??

A little confused, any ideas really apprecaited

Thanks in advance

Ian
ianinspainAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ozymandiasConnect With a Mentor Commented:
There are many ways of doing this.
You could get rid of the Open and Close methods and just open the file in the constructor and close it in the destructor instead of explicity opening and closing it for each write.
Are there are other applicatoins that might want to write to the log file while you app is running ?

You don't need to worry about consurrent sessoin access but you might want to make your log write process thread safe.

For your WriteError method you can do this :

public void WriteError{

    lock(this){
         //current body of method here
    }
}

this makes sure only one write can ocurr at a time
0
 
ianinspainAuthor Commented:
that sounds great,

lock(this)
{

//mycode

}

i have made everything static, do you think this is the best way to go...

The reason i am doing a OPEN and CLOSE on each write is that.... i am using the log in form of a logError_datetime.txt... so i need to create it each time..... its not a constant running log...

If lock(this) is the solution, that sounds great... you state i shouldn't worry about concurrenmt sessions .... but surely i should because its a web service so technically many many different users can be accessing the application/web service at the same time.....

I could of course... like i say just create a global variable to an instance of the log... but i don't know what i would benefit from this.... all of the classes need access to it....

It is only 1 app that will be using it


Thanks for your input

Ian
0
 
ozymandiasCommented:
Sorry, when I said don't worry about sessions I didn't mean they weren't a consideration, I meant that in concurrency terms it was more practical to think of things being accessed at the thread level.
If you make your code thread safe it is effectively session safe as a result.

I don't think there is any great benefit of creating a reference in the global.asax and storing it at applications level unless it is an expensive object to contsruct.
To access it each time you would still have to create a reference and then cast the applicatoin level object to the type of the reference so you're probably OK.
0
 
ianinspainAuthor Commented:
thanks
0
 
ozymandiasCommented:
If you have static methods that's fine, if you hve any static variables that you intend to write/alter in those methods you should make those methods thread safe also using the lock method.
If you have a single instance (static) variable that can be modified then theoretically it could be modified by two threads simultaneously.....which would be bad...probably....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.