473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Write log using static function

Hello everybody,

In my code, I wanted to write log exception to some file.
So I created a Utility class & wrote a static method in that to write log message to file.

Expand|Select|Wrap|Line Numbers
  1. public static void WriteLog(string message)
  2. {
  3.   using (FileStream fs = new FileStream(@"c:\log.txt",  FileMode.Append))
  4.   {
  5.      using (StreamWriter streamWriter = new StreamWriter(fs))
  6.      {
  7.           streamWriter.WriteLine(message);
  8.      }
  9.   }
  10. }
  11.  

This works fine so far but I fear if two methods call this function simultaneously.. what will happen?
Also, I want to access this same Utility library in my other "WEB" projects... will it work there too?

Or else.. what will be the best way to log exceptions in any project?

Thanks in advance.

Zack
Oct 30 '12 #1
4 3138
Frinavale
9,735 Expert Mod 8TB
I think that you will probably end up with an exception indicating that the file is already in use. Handle that exception in a way that makes sense in your application (for example, wait for a few seconds and try again).

You may be interested in checking out the C# lock Statement.

It ensures that one thread does not enter a critical section of code (the part where you are accessing the file) while another thread is in the critical section.

-Frinny
Oct 31 '12 #2
Thanks for the reply...

link was helpful.

But what about my other question, Will this static method work
fine in Web environment?

How many instances will get created when clients from different web browsers will put a request?

Thanks in Advance.

Zack
Oct 31 '12 #3
Frinavale
9,735 Expert Mod 8TB
Only one instance will get created and will be shared across all connections to your web application.

I have used static methods in web situations before and it is fine...just be aware of the implications (which I'm sure you are).

-Frinny
Nov 1 '12 #4
Thank You for ur response..
Nov 2 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
5
by: A_StClaire_ | last post by:
annoying one. can anyone spot the issue? it appears communityCards can't be accessed via my static function but I don't know why... Game.h: #pragma once #include <vector> #include...
12
by: dual0 | last post by:
Hello, I found some function like void static foo(...){ .... } what does the static keyword stand for? what is a static function?
40
by: vishnu | last post by:
Hi friend, i have a problem in my program what is the use of static function in C lang? plz help me
4
by: Jonathan Roewen | last post by:
Hi I've got loading assemblies dynamically done (wasn't too difficult). Now I want to lookup a static function in the loaded assembly, and if found, return it somehow, and call it from my app. So...
4
by: Kylin | last post by:
which situation should use the static function ? -- FireCrow Studio Kylin Garden EMail:w0wd@sohu.com ICQ:156134382
1
by: Homa | last post by:
Hi, It just come to my mine about conncurreny and static functions. This would not be a problem in window application because each thread has it own stack (and heap? forgot...). I'm using some...
18
by: Ronald Bruck | last post by:
I have several routines which are used millions of times in my programs, using the Gnu multi-precision software's floating-point reals (see <http://www.swox.se/gmp>). These are of type mpf_t, and...
4
by: John Doe | last post by:
Hi, I have a singleton class defined like this : class UIManager : public CSingleton<UIManager>, public CObject { protected: DECLARE_DYNAMIC(UIManager) friend class CSingleton<UIManager>;
7
by: Abhinay | last post by:
Hi there, I am developing server in java which accept client request in multi threaded fashion and response back, each client request will be process by separate dedicated server thread. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.