473,851 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Event logging into a single textfile by more than one users at a time

38 New Member
Hi ,
i would like to reopen the issue in the post http://www.gamedev.net/community/for...opic_id=418791.

My scenario is like this:

1. A web application is there (being created by some other company)

2. We develop the business layer and data access layer.


The web application calls our business class. Our business class in turn calls stored procedures and all and giving back result to web application.Now , Our business class uses a commonservice class which contains the code for logging, exception handling and all.

The problem is when we log execution information of our business class to a single location,say D:\logfile\logg ing.txt.When more than one user accesses the
the same file, some contention happens. Not all events are logged by all users.

To avoid this I wrote a new class for logging and added to commonservice class , which looks like:
Expand|Select|Wrap|Line Numbers
  1. Imports System.IO
  2. Public Class MyTextwriter
  3.  
  4.  
  5.     Private tw As System.IO.TextWriter
  6.  
  7.  
  8.     Public ReadOnly Property MyTextwriter() As System.IO.TextWriter
  9.         Get
  10.  
  11.             tw = System.IO.TextWriter.Synchronized(File.AppendText("D:\log\temp.txt"))
  12.  
  13.  
  14.             Return tw
  15.         End Get
  16.     End Property
  17.  
  18.  
  19.  
  20.  
  21. Public Sub Trace(ByVal message As String)
  22.  
  23.           tw = MyTextwriter
  24.  
  25.  
  26.         tw.Write(message)
  27.         tw.Flush()
  28.         tw .Close()
  29.  
  30.     End Sub 'Trace 
  31.  
  32.  
  33.  
  34.  
  35. End Class
  36.  
I tried testing this new logging class using a .net multithreaded application
which spawns 20 threads and each executes our business class method(which does all our business logic ) which in turn calls our logging class for logging the events to the same file at a time. But the problem still persist, an exception is thrown saying like:"The file D:\logfile\logg ing.txt is being used by some other process" .

Can anybody help me to solve this issue? Any ideas, hints, and help would be greatly appreciated..


Thanks.
jaleel
Sep 21 '07 #1
10 1806
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
Since the file can be read by only onbe process at a time
the best approach would be to funnel all the processes into one!!

A static / singleton class would do the magic.

Cheers
Sep 21 '07 #2
Plater
7,872 Recognized Expert Expert
Yeah, easy pie.
Use a static class.

Pretty much all of my projects now have a static class called "Logging"
I keep various methods to log data, log errors and to log anything I else I want.
Sep 21 '07 #3
jaleel
38 New Member
i thank u for ur reply..

I wil give it a try tomorrow as I'm on leave 2day.

I am really afraid that making the logging class as static will alone manage all
race conditions happenening when more than one threads are trying to log to the same file.Anyhow I will try with it and get back u with comments...

thanks ,jaleel
Sep 23 '07 #4
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
i thank u for ur reply..

I wil give it a try tomorrow as I'm on leave 2day.

I am really afraid that making the logging class as static will alone manage all
race conditions happenening when more than one threads are trying to log to the same file.Anyhow I will try with it and get back u with comments...

thanks ,jaleel
A static class will always maintain only one instance in the application.
So even if 2 threads attack the static class at almost the same time it will form a queue, So logging will be as normal.

Cheers
Sep 23 '07 #5
jaleel
38 New Member
Hi
i tried using a class with static members , but not worked well. But following code works fine for some extent.

Expand|Select|Wrap|Line Numbers
  1. Public Class Logger
  2.  
  3.  
  4.  
  5. Private loggingTimeStamp As DateTime 'non-static members 
  6.  
  7. Private messageLog As String
  8.  
  9. Private loggingFile As String
  10.  
  11. Private exceptionMessage As String
  12.  
  13. Private Shared tw As System.IO.TextWriter ' static variable
  14.  
  15. Private Shared locker As New Object
  16.  
  17. Public Sub Trace(ByVal message As String)
  18.  
  19. SyncLock locker
  20.  
  21. Try
  22.  
  23.  
  24. messageLog = message + DateTime.Now.ToString
  25.  
  26. .......................................
  27.  
  28. 'here some other manipulation like if logginfile is > 2MB rename it to some other name
  29.  
  30. 'can I put the part till the next statement ( i.e. initialising textwriter )outside the locker?
  31.  
  32. tw = System.IO.TextWriter.Synchronized(File.AppendText(loggingFile))
  33.  
  34.  
  35. tw.WriteLine(messageLog)
  36.  
  37. tw.Flush()
  38.  
  39.  
  40. Catch
  41.  
  42. exceptionMessage = "LoggingFileError"
  43.  
  44. Throw New Exception(exceptionMessage)
  45.  
  46. Finally
  47.  
  48. tw.Close()
  49.  
  50.  
  51. End Try
  52.  
  53. End SyncLock
  54.  
  55. End Sub 'Trace 
  56.  
  57. End Class

it works fine with 20 threads. But i tested using 50 threads and 100 threads . but some cases failed to log correctly say 2 to 10 cases . Theses amount of threads are not able to debug at a time. What could be the problem.. any suggestions?

Could anybody show some sample class which works fine..?

thanks in advance,
jaleel
Sep 28 '07 #6
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
Though this wouldnt help much but try making your classs a static one too.

If you still have issues follow the singleton pattern correctly and it should perform better

cheers
Sep 28 '07 #7
jaleel
38 New Member
hi shashi ,i think there is no static(i.e.shar ed) class in vb.net. Singleton means having only one object..right? need to try it.

thanks
Sep 28 '07 #8
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
hi shashi ,i think there is no static(i.e.shar ed) class in vb.net. Singleton means having only one object..right? need to try it.

thanks
Wow...is it?
C# Rulez :P
hahaha..well... there must be, try some online convertor to test it

if not i will try it tomorrow!

cheers
Sep 28 '07 #9
dip_developer
648 Recognized Expert Contributor
Wow...is it?
C# Rulez :P
hahaha..well... there must be, try some online convertor to test it

if not i will try it tomorrow!

cheers
Hi sashi......

vb.net also rulez as c#......there is Static class in VB.Net also......but the implementation is different....VB has an object named Module....you can add a module to your project and can write your Methods there....Module is a static class and doesn't need to be instantiated........

you can have a mudule named myGlobalModule and can access a method of that module named myGlobalMethod by simply writing
myGlobalModule.myGlobalMethod( ).

As far my knowledge goes....

Although differences do exist between VB and C#, both languages use the .NET Framework common language runtime and are equally powerful
Sep 28 '07 #10

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

Similar topics

1
418
by: Gram | last post by:
Hello, Can anoyone help me with the following: I have a application online using ASP in which users log in and log out. However, a lot of users simply close the window, bypassing my log-out script. I have tried using the Javascript onUnload function with success,but refreshing the screen also triggers this at the wrong time, Has anyone got any suggestions? Thanks in advance,
23
2237
by: Rotem | last post by:
Hi, while working on something in my current project I have made several improvements to the logging package in Python, two of them are worth mentioning: 1. addition of a logging record field %(function)s, which results in the name of the entity which logged the record. My version even deduces the class name in the case which the logger is a bound method, and assuming the name of the "self" variable is indeed "self".
5
2077
by: Michelle Stone | last post by:
Hi everybody I am writing a simple asp.net application using form authentication. I store the list of all users and their passwords in an SQL Server database table. My client recently told me that he wants me to do something through which only one user can login using any given account name. I mean to say, for example, when a user called "abc" is online, another person shouldn't be
29
15564
by: Patrick | last post by:
I have the following code, which regardless which works fine and logs to the EventViewer regardless of whether <processModel/> section of machine.config is set to username="SYSTEM" or "machine" ---Start of test.aspx---- <%@ Page language="C#" AutoEventWireup="false" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD>
5
6732
by: news.microsoft.com | last post by:
We are logging many events on our application but cant seem to discover how to determine who the user was in the session end event, as any session storage seems to have disapeared at the point at which the event fires. In essence, I want to log when a users session expires. Any inspiration would be appreciated.
0
1877
by: robert | last post by:
As more and more python packages are starting to use the bloomy (Java-ish) 'logging' module in a mood of responsibility and as I am not overly happy with the current "thickener" style of usage, I want to put this comment and a alternative most simple default framework for discussion. Maybe there are more Python users which like to see that imported (managed) logging issue more down-to-earth and flexible ? ... Vinay Sajip wrote: >...
0
918
by: =?Utf-8?B?S2Fp?= | last post by:
Hi all, as SQL-Storage is more expensive than general webspace I would like to write the log of my ASP.net 2005 application to a textfile. Would this be a problem with Enterprise Library 2.0? Because dozens of users might write to the log at the same time? Additionally I would like to use the Rolling File Trace listener (see http://bloggingabout.net/blogs/erwyn/articles/rolling_file_trace_listener.aspx) . Anyone having experience with this...
17
2759
by: Cramer | last post by:
I plan to implement an exception logging feature in an ASP.NET Web application that writes encountered exceptions to disk. The exception data will be stored as XML. I am planning on having each exception written to its own XML file. I plan to NOT append exceptions to a single XML file because I don't want for the exception logging component to have to deal with contention for access to the file. This is possible when, for example,...
4
1629
by: samwyse | last post by:
In the Python 2.5 Library Reference, section 14.5.3 (Logging to multiple destinations), an example is given of logging to both a file and the console. This is done by using logging.basicConfig() to configure a log file, and then calling logging.getLogger('').addHandler(console) to add the console. However, in section 14.5.4 (Sending and receiving logging events across a network), a call is made to rootLogger.addHandler(socketHandler),...
0
9897
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9747
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11019
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10670
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
5736
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5933
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4549
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4143
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.