Solved

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

Posted on 2006-11-08
5
199 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:ianinspain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 15

Accepted Solution

by:
ozymandias earned 500 total points
ID: 17899185
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
 

Author Comment

by:ianinspain
ID: 17899408
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
 
LVL 15

Expert Comment

by:ozymandias
ID: 17899838
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
 

Author Comment

by:ianinspain
ID: 17899864
thanks
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17899875
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question