3 Replies - 259 Views - Last Post: 26 July 2013 - 06:23 AM Rate Topic: -----

#1 albert018  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 07-January 13

Calling the same method in a static class.

Posted 25 July 2013 - 08:38 AM

Sometimes I might need to record some logs into a text file.

I might use 「System.IO.File.AppendAllLines」 or 「FileStream」 to write the logs into the text file.

But it doesn't work when there are too much users in the meantime.

I found out that the text file would be locked because of too many users trying to write it.

So my coworker suggest me to design a static class, then implement a static method to write the log.

Even there are many users call this method at the same time, it would be called in sequence.

Then the text file won't be locked.

I am just curious whether this saying is correct?

This post has been edited by JackOfAllTrades: 25 July 2013 - 09:21 AM
Reason for edit:: Removed large font


Is This A Good Question/Topic? 0
  • +

Replies To: Calling the same method in a static class.

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,428
  • Joined: 23-August 08

Re: Calling the same method in a static class.

Posted 25 July 2013 - 09:21 AM

log4net
Was This Post Helpful? 2
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3467
  • View blog
  • Posts: 10,687
  • Joined: 05-May 12

Re: Calling the same method in a static class.

Posted 25 July 2013 - 05:08 PM

View Postalbert018, on 25 July 2013 - 11:38 AM, said:

So my coworker suggest me to design a static class, then implement a static method to write the log.

Even there are many users call this method at the same time, it would be called in sequence.

Then the text file won't be locked.

I am just curious whether this saying is correct?

Unfortunately, this is not correct. It is only the static constructor that is guaranteed to be called in a thread safe manner. The other methods of your (static) class will not be be thread safe.
Was This Post Helpful? 1
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4433
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: Calling the same method in a static class.

Posted 26 July 2013 - 06:23 AM

I'm hoping that the guy suggested using a Singleton and making the actual write operation a critical section. But JackOfAllTrades is totally right; don't write your own logging implementation; you'll probably not do it as well as the numerous options available to you already. log4net, elmah, Enterprise Library...just search on Nuget and use one of them. I'd probably go with log4net.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1