473,503 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ????

Hi,

I have a Web application in ASP.NET. My Application allows the users upload
files into the server after checking their user names and passwords. For
each transaction, the Web program will write the information about user
name, filename upload, filesize, date and time of uploading into the log
file. (The name of the log file is constructed by Current Year and Current
Month in my program). Is there any problems with writing into the log file
if there are multi-users access on the same page to upload files ????. My
program works OK with the code below, but I don't know if there is any
problems when multi-users perform writing into the same log file at the same
time ??? Here is a part of my code to open the log file for editing :

'WRITE LO LOG FILE
Dim strFile as string= CurrYear & CurrMonth
Dim LogFile As String = Server.MapPath("LOGS") & "\" & strFile &
".log"

Dim aLogfile As FileInfo = New FileInfo(LogFile)
If aLogfile.Exists Then
Dim objStreamWriter as StreamWriter
objStreamWriter = File.AppendText(LogFile)
Dim strLine as string= CurrYear & CurrMonth & CurrDay &
CurrHour & CurrMin & CurrSec & " " & radioFileUp.selectedValue & " " &
Session("UserID") & " " & Carrier & " " & fn & " " & fsize & " bytes "
objStreamWriter.WriteLine(strLine)
objStreamWriter.Close()
Else
Dim objStreamWriter as StreamWriter = File.CreateText(LogFile)
Dim strLine as string= CurrYear & CurrMonth & CurrDay &
CurrHour & CurrMin & CurrSec & " " & radioFileUp.selectedValue & " " &
Session("UserID") & " " & Carrier & " " & fn & " " & fsize & " bytes "
objStreamWriter.WriteLine(strLine)
objStreamWriter.Close()
End If

Please give me some advises. Do I need to change anything in my code for it
?. Thanks in advance.
Nov 19 '05 #1
4 2165
Yes...you can have a file lock condition. You'll need to serialize access
to the code. I think in VB.Net you can wrap the offending code in a
SynchLock statement:
http://msdn.microsoft.com/library/de...tmSyncLock.asp

Also, why reinvent the wheel?
http://logging.apache.org/log4net/

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"HNguyen" <bi********@yahoo.com> wrote in message
news:%2******************@TK2MSFTNGP14.phx.gbl...
Hi,

I have a Web application in ASP.NET. My Application allows the users upload files into the server after checking their user names and passwords. For
each transaction, the Web program will write the information about user
name, filename upload, filesize, date and time of uploading into the log
file. (The name of the log file is constructed by Current Year and Current
Month in my program). Is there any problems with writing into the log file if there are multi-users access on the same page to upload files ????. My
program works OK with the code below, but I don't know if there is any
problems when multi-users perform writing into the same log file at the same time ??? Here is a part of my code to open the log file for editing :

'WRITE LO LOG FILE
Dim strFile as string= CurrYear & CurrMonth
Dim LogFile As String = Server.MapPath("LOGS") & "\" & strFile &
".log"

Dim aLogfile As FileInfo = New FileInfo(LogFile)
If aLogfile.Exists Then
Dim objStreamWriter as StreamWriter
objStreamWriter = File.AppendText(LogFile)
Dim strLine as string= CurrYear & CurrMonth & CurrDay &
CurrHour & CurrMin & CurrSec & " " & radioFileUp.selectedValue & " " &
Session("UserID") & " " & Carrier & " " & fn & " " & fsize & " bytes "
objStreamWriter.WriteLine(strLine)
objStreamWriter.Close()
Else
Dim objStreamWriter as StreamWriter = File.CreateText(LogFile)
Dim strLine as string= CurrYear & CurrMonth & CurrDay &
CurrHour & CurrMin & CurrSec & " " & radioFileUp.selectedValue & " " &
Session("UserID") & " " & Carrier & " " & fn & " " & fsize & " bytes "
objStreamWriter.WriteLine(strLine)
objStreamWriter.Close()
End If

Please give me some advises. Do I need to change anything in my code for it ?. Thanks in advance.

Nov 19 '05 #2
Hi Karl,

I've tried to add SyncLock and EndLock. The code looks like this :

If aLogfile.Exists Then

Dim objStreamWriter as StreamWriter
objStreamWriter = File.AppendText(LogFile)
Dim strLine as string= CurrYear & CurrMonth & CurrDay &
CurrHour & CurrMin & CurrSec & " " & radioFileUp.selectedValue & " " &
Session("UserID") & " " & Carrier & " " & fn & " " & fsize & " bytes "

SyncLock objStreamWriter.WriteLine(strLine)
End SyncLock
objStreamWriter.Close()

Else

Dim objStreamWriter as StreamWriter = File.CreateText(LogFile)
Dim strLine as string= CurrYear & CurrMonth & CurrDay &
CurrHour & CurrMin & CurrSec & " " & radioFileUp.selectedValue & " " &
Session("UserID") & " " & Carrier & " " & fn & " " & fsize & " bytes "

SyncLock objStreamWriter.WriteLine(strLine)
End SyncLock
objStreamWriter.Close()

End If

I've got the error like this :

Compilation Error
Description: An error occurred during the compilation of a resource required
to service this request. Please review the following specific error details
and modify your source code appropriately.

Compiler Error Message: BC30491: Expression does not produce a value.

Source Error:
Line 202: Dim strLine as string= CurrYear & CurrMonth & CurrDay &
CurrHour & CurrMin & CurrSec & " " & radioFileUp.selectedValue & " " &
Session("UserID") & " " & Carrier & " " & fn & " " & fsize & " bytes "
Line 203:
Line 204: SyncLock objStreamWriter.WriteLine(strLine)
Line 205: End SyncLock
Line 206: objStreamWriter.Close()

Any suggestions ??? Thanks.

====================================

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:OP**************@TK2MSFTNGP10.phx.gbl...
Yes...you can have a file lock condition. You'll need to serialize access to the code. I think in VB.Net you can wrap the offending code in a
SynchLock statement:
http://msdn.microsoft.com/library/de...tmSyncLock.asp
Also, why reinvent the wheel?
http://logging.apache.org/log4net/

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"HNguyen" <bi********@yahoo.com> wrote in message
news:%2******************@TK2MSFTNGP14.phx.gbl...
Hi,

I have a Web application in ASP.NET. My Application allows the users upload
files into the server after checking their user names and passwords. For each transaction, the Web program will write the information about user
name, filename upload, filesize, date and time of uploading into the log
file. (The name of the log file is constructed by Current Year and Current Month in my program). Is there any problems with writing into the log

file
if there are multi-users access on the same page to upload files ????. My program works OK with the code below, but I don't know if there is any
problems when multi-users perform writing into the same log file at the

same
time ??? Here is a part of my code to open the log file for editing :
'WRITE LO LOG FILE
Dim strFile as string= CurrYear & CurrMonth
Dim LogFile As String = Server.MapPath("LOGS") & "\" & strFile &
".log"

Dim aLogfile As FileInfo = New FileInfo(LogFile)
If aLogfile.Exists Then
Dim objStreamWriter as StreamWriter
objStreamWriter = File.AppendText(LogFile)
Dim strLine as string= CurrYear & CurrMonth & CurrDay &
CurrHour & CurrMin & CurrSec & " " & radioFileUp.selectedValue & " " &
Session("UserID") & " " & Carrier & " " & fn & " " & fsize & " bytes "
objStreamWriter.WriteLine(strLine)
objStreamWriter.Close()
Else
Dim objStreamWriter as StreamWriter = File.CreateText(LogFile)
Dim strLine as string= CurrYear & CurrMonth & CurrDay &
CurrHour & CurrMin & CurrSec & " " & radioFileUp.selectedValue & " " &
Session("UserID") & " " & Carrier & " " & fn & " " & fsize & " bytes "
objStreamWriter.WriteLine(strLine)
objStreamWriter.Close()
End If

Please give me some advises. Do I need to change anything in my code

for it
?. Thanks in advance.


Nov 19 '05 #3
SynchLock
objStreamWriter.WriteLIne(strLine)
end SynchLock

(don't put them on the same line).

Also, you probabably need to wrap the entire File.AppendText(LogFile) in
there...and you could use some exception handling:

Dim objStreamWriter as StreamWriter
Dim strLine as string= CurrYear & CurrMonth & CurrDay & CurrHour &
CurrMin & CurrSec & " " & radioFileUp.selectedValue & " " &
Session("UserID") & " " & Carrier & " " & fn & " " & fsize & " bytes "
SyncLock
try
objStreamWriter = File.AppendText(LogFile)
objStreamWriter.WriteLine(strLine)
finally
if not objStreamWriter is nothing then
objStreamWriter.Close()
end if
end try
End SyncLock
Finally, your code should be refactored...the If and Else have too much
repeated code (ie, the strLine, the WriteLine, the associated exception
handling and locking)...all that causes you is (a) more time to write it (b)
more time to change it (c) more time to fix it (d) more likely to have bugs
(e) more likely to introduce bugs....refactoring is your friend

karl


--
MY ASP.Net tutorials
http://www.openmymind.net/
"HNguyen" <bi********@yahoo.com> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl...
Hi Karl,

I've tried to add SyncLock and EndLock. The code looks like this :

If aLogfile.Exists Then

Dim objStreamWriter as StreamWriter
objStreamWriter = File.AppendText(LogFile)
Dim strLine as string= CurrYear & CurrMonth & CurrDay &
CurrHour & CurrMin & CurrSec & " " & radioFileUp.selectedValue & " " &
Session("UserID") & " " & Carrier & " " & fn & " " & fsize & " bytes "

SyncLock objStreamWriter.WriteLine(strLine)
End SyncLock
objStreamWriter.Close()

Else

Dim objStreamWriter as StreamWriter = File.CreateText(LogFile)
Dim strLine as string= CurrYear & CurrMonth & CurrDay &
CurrHour & CurrMin & CurrSec & " " & radioFileUp.selectedValue & " " &
Session("UserID") & " " & Carrier & " " & fn & " " & fsize & " bytes "

SyncLock objStreamWriter.WriteLine(strLine)
End SyncLock
objStreamWriter.Close()

End If

I've got the error like this :

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30491: Expression does not produce a value.

Source Error:
Line 202: Dim strLine as string= CurrYear & CurrMonth & CurrDay &
CurrHour & CurrMin & CurrSec & " " & radioFileUp.selectedValue & " " &
Session("UserID") & " " & Carrier & " " & fn & " " & fsize & " bytes "
Line 203:
Line 204: SyncLock objStreamWriter.WriteLine(strLine)
Line 205: End SyncLock
Line 206: objStreamWriter.Close()

Any suggestions ??? Thanks.

====================================

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:OP**************@TK2MSFTNGP10.phx.gbl...
Yes...you can have a file lock condition. You'll need to serialize access
to the code. I think in VB.Net you can wrap the offending code in a
SynchLock statement:

http://msdn.microsoft.com/library/de...tmSyncLock.asp

Also, why reinvent the wheel?
http://logging.apache.org/log4net/

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"HNguyen" <bi********@yahoo.com> wrote in message
news:%2******************@TK2MSFTNGP14.phx.gbl...
Hi,

I have a Web application in ASP.NET. My Application allows the users

upload
files into the server after checking their user names and passwords. For each transaction, the Web program will write the information about user name, filename upload, filesize, date and time of uploading into the log file. (The name of the log file is constructed by Current Year and Current Month in my program). Is there any problems with writing into the log

file
if there are multi-users access on the same page to upload files ????. My program works OK with the code below, but I don't know if there is any
problems when multi-users perform writing into the same log file at the
same
time ??? Here is a part of my code to open the log file for
editing :
'WRITE LO LOG FILE
Dim strFile as string= CurrYear & CurrMonth
Dim LogFile As String = Server.MapPath("LOGS") & "\" & strFile

& ".log"

Dim aLogfile As FileInfo = New FileInfo(LogFile)
If aLogfile.Exists Then
Dim objStreamWriter as StreamWriter
objStreamWriter = File.AppendText(LogFile)
Dim strLine as string= CurrYear & CurrMonth & CurrDay &
CurrHour & CurrMin & CurrSec & " " & radioFileUp.selectedValue & " " & Session("UserID") & " " & Carrier & " " & fn & " " & fsize & " bytes " objStreamWriter.WriteLine(strLine)
objStreamWriter.Close()
Else
Dim objStreamWriter as StreamWriter = File.CreateText(LogFile) Dim strLine as string= CurrYear & CurrMonth & CurrDay &
CurrHour & CurrMin & CurrSec & " " & radioFileUp.selectedValue & " " & Session("UserID") & " " & Carrier & " " & fn & " " & fsize & " bytes " objStreamWriter.WriteLine(strLine)
objStreamWriter.Close()
End If

Please give me some advises. Do I need to change anything in my code

for
it
?. Thanks in advance.



Nov 19 '05 #4
Thank you Kark. I tried it and it worked. Only after SyncLock , I needed to
have the Object name. If not, I got the error.

===============================
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:OF**************@TK2MSFTNGP14.phx.gbl...
SynchLock
objStreamWriter.WriteLIne(strLine)
end SynchLock

(don't put them on the same line).

Also, you probabably need to wrap the entire File.AppendText(LogFile) in
there...and you could use some exception handling:

Dim objStreamWriter as StreamWriter
Dim strLine as string= CurrYear & CurrMonth & CurrDay & CurrHour &
CurrMin & CurrSec & " " & radioFileUp.selectedValue & " " &
Session("UserID") & " " & Carrier & " " & fn & " " & fsize & " bytes "
SyncLock
try
objStreamWriter = File.AppendText(LogFile)
objStreamWriter.WriteLine(strLine)
finally
if not objStreamWriter is nothing then
objStreamWriter.Close()
end if
end try
End SyncLock
Finally, your code should be refactored...the If and Else have too much
repeated code (ie, the strLine, the WriteLine, the associated exception
handling and locking)...all that causes you is (a) more time to write it (b) more time to change it (c) more time to fix it (d) more likely to have bugs (e) more likely to introduce bugs....refactoring is your friend

karl


--
MY ASP.Net tutorials
http://www.openmymind.net/
"HNguyen" <bi********@yahoo.com> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl...
Hi Karl,

I've tried to add SyncLock and EndLock. The code looks like this :

If aLogfile.Exists Then

Dim objStreamWriter as StreamWriter
objStreamWriter = File.AppendText(LogFile)
Dim strLine as string= CurrYear & CurrMonth & CurrDay &
CurrHour & CurrMin & CurrSec & " " & radioFileUp.selectedValue & " " &
Session("UserID") & " " & Carrier & " " & fn & " " & fsize & " bytes "

SyncLock objStreamWriter.WriteLine(strLine)
End SyncLock
objStreamWriter.Close()

Else

Dim objStreamWriter as StreamWriter = File.CreateText(LogFile)
Dim strLine as string= CurrYear & CurrMonth & CurrDay &
CurrHour & CurrMin & CurrSec & " " & radioFileUp.selectedValue & " " &
Session("UserID") & " " & Carrier & " " & fn & " " & fsize & " bytes "

SyncLock objStreamWriter.WriteLine(strLine)
End SyncLock
objStreamWriter.Close()

End If

I've got the error like this :

Compilation Error
Description: An error occurred during the compilation of a resource required
to service this request. Please review the following specific error

details
and modify your source code appropriately.

Compiler Error Message: BC30491: Expression does not produce a value.

Source Error:
Line 202: Dim strLine as string= CurrYear & CurrMonth & CurrDay &
CurrHour & CurrMin & CurrSec & " " & radioFileUp.selectedValue & " " &
Session("UserID") & " " & Carrier & " " & fn & " " & fsize & " bytes "
Line 203:
Line 204: SyncLock objStreamWriter.WriteLine(strLine)
Line 205: End SyncLock
Line 206: objStreamWriter.Close()

Any suggestions ??? Thanks.

====================================

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:OP**************@TK2MSFTNGP10.phx.gbl...
Yes...you can have a file lock condition. You'll need to serialize

access
to the code. I think in VB.Net you can wrap the offending code in a
SynchLock statement:

http://msdn.microsoft.com/library/de...tmSyncLock.asp

Also, why reinvent the wheel?
http://logging.apache.org/log4net/

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"HNguyen" <bi********@yahoo.com> wrote in message
news:%2******************@TK2MSFTNGP14.phx.gbl...
> Hi,
>
> I have a Web application in ASP.NET. My Application allows the users upload
> files into the server after checking their user names and passwords.

For
> each transaction, the Web program will write the information about user > name, filename upload, filesize, date and time of uploading into the log > file. (The name of the log file is constructed by Current Year and

Current
> Month in my program). Is there any problems with writing into the log file
> if there are multi-users access on the same page to upload files ????.
My
> program works OK with the code below, but I don't know if there is
any > problems when multi-users perform writing into the same log file at

the same
> time ??? Here is a part of my code to open the log file for editing
:
>
> 'WRITE LO LOG FILE
> Dim strFile as string= CurrYear & CurrMonth
> Dim LogFile As String = Server.MapPath("LOGS") & "\" & strFile

& > ".log"
>
> Dim aLogfile As FileInfo = New FileInfo(LogFile)
> If aLogfile.Exists Then
> Dim objStreamWriter as StreamWriter
> objStreamWriter = File.AppendText(LogFile)
> Dim strLine as string= CurrYear & CurrMonth & CurrDay &
> CurrHour & CurrMin & CurrSec & " " & radioFileUp.selectedValue & "
" & > Session("UserID") & " " & Carrier & " " & fn & " " & fsize & "
bytes
" > objStreamWriter.WriteLine(strLine)
> objStreamWriter.Close()
> Else
> Dim objStreamWriter as StreamWriter = File.CreateText(LogFile) > Dim strLine as string= CurrYear & CurrMonth & CurrDay &
> CurrHour & CurrMin & CurrSec & " " & radioFileUp.selectedValue & "
"
& > Session("UserID") & " " & Carrier & " " & fn & " " & fsize & "
bytes
" > objStreamWriter.WriteLine(strLine)
> objStreamWriter.Close()
> End If
>
> Please give me some advises. Do I need to change anything in my code

for
it
> ?. Thanks in advance.
>
>



Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1962
by: Mikael Olofsson | last post by:
Hi all! Sorry for this rather lengthy post. It's a long preamble followed by a few questions based on that. Here's my situation: I am trying to write a specialized scanner utility for my own...
0
2555
by: Blah Blah | last post by:
i just thought i'd shoot out a quick email on problems i've been having with utf-8 in moving from 4.1.0 to 4.1.1. (please note that because i am using UTF-8 as my default character set, i compiled...
0
1776
by: Emine Ekin | last post by:
/*Apologize for multiple posts*/ FIRST CALL FOR PAPERS ADVIS 2006 Fourth Biennial International Conference on Advances in Information Systems 18-20 October, 2006 Izmir, Turkey
5
1688
by: Rob Durant | last post by:
Hi, I have a multi-threaded application (have also tried as service - same behaviour) that runs fine on XP, but not on 2003. Symptoms are: Threads are started normally, locks acquired and...
2
5224
by: Tim V. | last post by:
Here's the layout: AIX v5.2, DB2 v8 fp8 running in 64bit I've got a Multi-partitioned db running on lpar4 and I want to connect it to 2 instances running on lpar13. We'll deal with just 1...
16
1538
by: Justin Koivisto | last post by:
I am trying to create a query to use as a report record source. Below is what I want to do (this was tested and works with a MySQL web script): SELECT contacts.id, contacts.email,...
1
3403
by: Chris LaJoie | last post by:
Hi, I have an app that runs many simultaneous threads. I have noticed (took me a while to figure out what it was doing though) that sometimes if there is a problem, the thread will simply...
4
1259
by: Max | last post by:
Playing around with multi-threading programs and ran into this little problem that maybe someone here could explain... Basically I have a class which launches a form object. Once the form is...
0
2178
by: Franklin M. Gauer III | last post by:
Hi, We have an application running that uses FULL SCREEN ANCHORING in Windows Forms. The application runs fine on all of our desktops and some laptops. We are having problems with certain Dell...
16
2505
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
0
7271
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,...
1
6979
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...
0
5570
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,...
1
4998
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...
0
3160
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...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1498
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 ...
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
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...

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.