473,385 Members | 1,325 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,385 software developers and data experts.

Logging to Text File in a Windows Service problems

7
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.IOException: The process cannot access the file '\\DEVMACH\Sandbox\BLAH.log' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamWriter.CreateFile(String path, Boolean append)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
at System.IO.StreamWriter..ctor(String path, Boolean append)
at TEST.CommonLibrary.MYAPP.ApplicationFileLogger.Wri teLine(String text, Boolean logDate)
at TEST.MYAPP.Services.ProcessingService.MyProcessing Service.DoProcessing()



' This is the in the service onstart event

_logFile = New ApplicationFileLogger("\\DEVMACH\Sandbox\BLAH.log" )

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

m_timer.Enabled = True
m_timer.Start()

GC.KeepAlive(m_timer)
WriteToTraceFile("The service has started.", .LogMaximum)

' end

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

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

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

Private Sub DoSomething()
WriteToTraceFile("DoSomething")
DoSomemore()
End Sub

Private Sub DoSomemore()
WriteToTraceFile("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 ApplicationFileLogger class.

public void WriteLine(String text, bool logDate)
{
// open the file for append (creates if not exist)
using (StreamWriter sw = new StreamWriter(_fileName, true))
{
// write the text and close the file
sw.WriteLine(text);
sw.Close();
}
}
Feb 21 '08 #1
2 3042
Shashi Sadasivan
1,435 Expert 1GB
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
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
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...
6
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...
16
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...
8
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...
2
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...
9
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...
3
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...
17
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.