Solved

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

Posted on 2006-11-08
5
195 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
  • 3
  • 2
5 Comments
 
LVL 15

Accepted Solution

by:
ozymandias earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks
0
 
LVL 15

Expert Comment

by:ozymandias
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now