469,927 Members | 1,777 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

lock file in ASP.NET

I am writing a Logging util for my ASP.NET application.
I am facing mulit process problem.

I developed a class LogFactory, and have a method called Get_Logger to
create a FileLogger, which will write text into a text file.

// sample code

FileLogger logger = LogFactory.Get_Logger();
logger.Log("Message here");
logger = null;

I read a lot of example that when we face the mulit process problem, we can
use the keyword "lock" to solve the problem
// example
File logger logger = LogFactory.Get_Logger();
lock (typeof (logger))
{
log.Log("Message here");
}
logger = null;

although a lot of article said that DONT use lock(typeof(logger)) because it
is a slow process and it will lock all objects, which in the same
application, and used in other process. However, I think in my case, I can
use typeof(logger) because I am try to write the message in a single text
file, no matter which process a instancing the logger object, I only need to
lock the text file, write text, release it and then other process repeat the
same step, right?

So my code is correct?? or any other better solution? Thanks
Oct 30 '07 #1
2 2427
Nothing wrong with using Lock. Especially when you are using it for as
minimal of sections of code as possible. However, there are two things I'd
recommend changing. First, use lock(typeof(FileLogger)) calling typeof on
an actual object instead of a type name should throw a compiler error.
Second, I would move the lock within the Log method itself. If you need the
operation to always be threadsafe then the operation itself should be
responsible to ensure that it is. As you have it written now, you have no
guarantee of thread safety. If you forget to lock even one call anywhere to
the log method you could have issues.

--
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com
"WingSiu" <榮少@上屎公司.*輕銤rote in message
news:eo**************@TK2MSFTNGP05.phx.gbl...
>I am writing a Logging util for my ASP.NET application.
I am facing mulit process problem.

I developed a class LogFactory, and have a method called Get_Logger to
create a FileLogger, which will write text into a text file.

// sample code

FileLogger logger = LogFactory.Get_Logger();
logger.Log("Message here");
logger = null;

I read a lot of example that when we face the mulit process problem, we
can use the keyword "lock" to solve the problem
// example
File logger logger = LogFactory.Get_Logger();
lock (typeof (logger))
{
log.Log("Message here");
}
logger = null;

although a lot of article said that DONT use lock(typeof(logger)) because
it is a slow process and it will lock all objects, which in the same
application, and used in other process. However, I think in my case, I
can use typeof(logger) because I am try to write the message in a single
text file, no matter which process a instancing the logger object, I only
need to lock the text file, write text, release it and then other process
repeat the same step, right?

So my code is correct?? or any other better solution? Thanks
Oct 30 '07 #2
Hi Andrew

After I read some website, now the code is changed like as follow

inside FileLogger,

private static readonly object = new object();

public void Log()
{
lock (object)
{
WriteLogToTextFile();
}
}

I am not sure why using static, but they always said lock a static object
should be better than lock a type.

Thanks

"Andrew Faust" <an****@andrewfaust.com>
???????:DE**********************************@micro soft.com...
Nothing wrong with using Lock. Especially when you are using it for as
minimal of sections of code as possible. However, there are two things I'd
recommend changing. First, use lock(typeof(FileLogger)) calling typeof on
an actual object instead of a type name should throw a compiler error.
Second, I would move the lock within the Log method itself. If you need
the operation to always be threadsafe then the operation itself should be
responsible to ensure that it is. As you have it written now, you have no
guarantee of thread safety. If you forget to lock even one call anywhere
to the log method you could have issues.

--
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com
"WingSiu" <榮少@上屎公司.*輕銤rote in message
news:eo**************@TK2MSFTNGP05.phx.gbl...
>>I am writing a Logging util for my ASP.NET application.
I am facing mulit process problem.

I developed a class LogFactory, and have a method called Get_Logger to
create a FileLogger, which will write text into a text file.

// sample code

FileLogger logger = LogFactory.Get_Logger();
logger.Log("Message here");
logger = null;

I read a lot of example that when we face the mulit process problem, we
can use the keyword "lock" to solve the problem
// example
File logger logger = LogFactory.Get_Logger();
lock (typeof (logger))
{
log.Log("Message here");
}
logger = null;

although a lot of article said that DONT use lock(typeof(logger)) because
it is a slow process and it will lock all objects, which in the same
application, and used in other process. However, I think in my case, I
can use typeof(logger) because I am try to write the message in a single
text file, no matter which process a instancing the logger object, I only
need to lock the text file, write text, release it and then other process
repeat the same step, right?

So my code is correct?? or any other better solution? Thanks

Oct 30 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Antoon Pardon | last post: by
14 posts views Thread by Gary Nelson | last post: by
4 posts views Thread by muttu2244 | last post: by
12 posts views Thread by Elmo M鄚tynen | last post: by
5 posts views Thread by pgdown | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.