Solved

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

Posted on 2006-11-08
5
197 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
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

777 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