473,670 Members | 2,324 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Logging to Text File in a Windows Service problems

7 New Member
I've wrote a windows service that performs simple functions within our application. To ensure safe running of the service - if it errors we want to know where when how - it logs to a text file - in this instance BLAH.log.

The code is executed within a timer which runs at 30 second intervals and polls folders. Each part of the process will log to the log file.

The service will throw an IOException exception. See below for this and my code sample (in a nutshell).

If I strip the code out and put in in a stand alone EXE it works fine. No exceptions are thrown.

Am I missing something in my code or has anyone else had issues in this kind of scenario?

Do we need to use the GC.KeepAlive call to ensure timer does not get destroyed (see below) ?

Andez



System.IO.IOExc eption: The process cannot access the file '\\DEVMACH\Sand box\BLAH.log' because it is being used by another process.
at System.IO.__Err or.WinIOError(I nt32 errorCode, String maybeFullPath)
at System.IO.FileS tream.Init(Stri ng path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIB UTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileS tream..ctor(Str ing path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.Strea mWriter.CreateF ile(String path, Boolean append)
at System.IO.Strea mWriter..ctor(S tring path, Boolean append, Encoding encoding, Int32 bufferSize)
at System.IO.Strea mWriter..ctor(S tring path, Boolean append)
at TEST.CommonLibr ary.MYAPP.Appli cationFileLogge r.WriteLine(Str ing text, Boolean logDate)
at TEST.MYAPP.Serv ices.Processing Service.MyProce ssingService.Do Processing()



' This is the in the service onstart event

_logFile = New ApplicationFile Logger("\\DEVMA CH\Sandbox\BLAH .log")

m_timer = New System.Timers.T imer(5000)
AddHandler m_timer.Elapsed , AddressOf Timer_Tick

m_timer.Enabled = True
m_timer.Start()

GC.KeepAlive(m_ timer)
WriteToTraceFil e("The service has started.", .LogMaximum)

' end

' 1 wrapper to log to the text file - this is a seperate DLL
Private Sub WriteToTraceFil e(ByVal text As String)
_logFile.WriteL ine(text, True)
End Sub

Private Sub Timer_Tick(ByVa l sender As Object, ByVal e As System.Timers.E lapsedEventArgs )
Try
m_timer.Enabled = False
DoSomething()

Catch ex As Exception
WriteToTraceFil e("The application has hit an exception")
Finally
m_timer.Enabled = True
End Try
End Sub

Private Sub DoSomething()
WriteToTraceFil e("DoSomething" )
DoSomemore()
End Sub

Private Sub DoSomemore()
WriteToTraceFil e("DoSomemore ")
...
End Sub



' And finally...
' This is the sample code from C# dll to write to the log file. The file name is
' passed into the constructor and stored in _fileName. This is in the ApplicationFile Logger class.

public void WriteLine(Strin g text, bool logDate)
{
// open the file for append (creates if not exist)
using (StreamWriter sw = new StreamWriter(_f ileName, true))
{
// write the text and close the file
sw.WriteLine(te xt);
sw.Close();
}
}
Feb 21 '08 #1
2 3064
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
This might be beacsue there are more than one instance writing to the file at the same time.

make the Write method a static one, which should sought out that issue
Feb 22 '08 #2
Andez
7 New Member
Shashi,

Thanks. Seems to have done the trick as it has been running without any problems.

Andez
Feb 27 '08 #3

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

Similar topics

6
3792
by: JT | last post by:
i have a text file that is used as a logfile for easier debugging. i am using the OpenTextFile method to write to this file. this method exists inside a logging function that can be called from anywhere in my ASP. this works great for our development system b/c we have a limited number of users. however, we're unable to use this logging mechanism on live due to concurrency issues. when two users attempt to open and write to the text...
6
9961
by: Kevin Jackson | last post by:
Let's say we log exceptions to the windows application event log using the Exception Management Application Block. Is there a pattern that can be used so the exception is logged only once and not everytime up through the call chain? Is there anything a caller can key off of to check to see if it should also log the exception?
16
2162
by: Einar Høst | last post by:
Hi, I'm getting into the Trace-functionality in .NET, using it to provide some much-needed logging across dlls in the project we're working on. However, being a newbie, I'm wondering if some more experienced loggers can provide me with some ideas as to how to log in a simple yet flexible manner. For instance, I'd like the code to be as uncluttered as possible by Trace statements. As an example of basic logging functionality, I've come...
8
1689
by: A.M | last post by:
Hi, What would be the best approach to log different items (SQL, Exceptions, Security, ...) I have a simple Logging function that adds log string to the end of a text file. Is there any application block for that? Should i do the logging into SQL server table ? Any help would be apprecited,
2
1085
by: cj | last post by:
I'd like some advise on a fool proof logging system. I have a windows app and need key events to be logged to a text file. Here are the REQUIREMENTS I have been given--generally requirements I'm given are non-negotiable. --It might be hours or days between logging events or just split seconds. --This application will be run from a network drive and the log file will reside in the directory it is run from.
9
2231
by: Claudio Grondi | last post by:
I am aware, that it is maybe the wrong group to ask this question, but as I would like to know the history of past file operations from within a Python script I see a chance, that someone in this group was into it already and is so kind to share here his experience. I have put already much efforts into this subject googling around, but up to now in vain. Best option I encountered yet is usage of the Greyware 'System Change Log' service...
3
5394
by: nicholas.petrella | last post by:
I am currently trying to use the python logging system as a core enterprise level logging solution for our development and production environments. The rotating file handler seems to be what I am looking for as I want the ability to have control over the number and size of log files that are written out for each of our tools. I have noticed a few problems with this handler and wanted to post here to get your impressions and possibly...
17
2742
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,...
0
1038
by: veerusrahul | last post by:
Hi, I am developing a windows service for FTP communication. This service will be installed in WindowsXPe environment where spaces is abt 2GB. i want to implement rolling file appender logging. I tried Log4net i am getting problems. Please suggest if i can use some other logging
0
8814
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...
1
8592
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7419
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5684
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4211
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
4391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2800
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
2042
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1794
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.