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

Discussion in 'ASP .Net' started by HNguyen, Dec 10, 2004.

  1. HNguyen

    HNguyen Guest

    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.
     
    HNguyen, Dec 10, 2004
    #1
    1. Advertising

  2. HNguyen

    Karl Seguin Guest

    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/default.asp?url=/library/en-us/vblr7/html/vastmSyncLock.asp

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

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "HNguyen" <> wrote in message
    news:%23FU15%...
    > 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.
    >
    >
     
    Karl Seguin, Dec 10, 2004
    #2
    1. Advertising

  3. HNguyen

    HNguyen Guest

    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:...
    > 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/default.asp?url=/library/en-us/vblr7/html/vastmSyncLock.asp
    >
    > Also, why reinvent the wheel?
    > http://logging.apache.org/log4net/
    >
    > Karl
    >
    > --
    > MY ASP.Net tutorials
    > http://www.openmymind.net/
    >
    >
    > "HNguyen" <> wrote in message
    > news:%23FU15%...
    > > 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.
    > >
    > >

    >
    >
     
    HNguyen, Dec 10, 2004
    #3
  4. HNguyen

    Karl Seguin Guest

    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" <> wrote in message
    news:eES$...
    > 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:...
    > > 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/default.asp?url=/library/en-us/vblr7/html/vastmSyncLock.asp
    > >
    > > Also, why reinvent the wheel?
    > > http://logging.apache.org/log4net/
    > >
    > > Karl
    > >
    > > --
    > > MY ASP.Net tutorials
    > > http://www.openmymind.net/
    > >
    > >
    > > "HNguyen" <> wrote in message
    > > news:%23FU15%...
    > > > 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.
    > > >
    > > >

    > >
    > >

    >
    >
     
    Karl Seguin, Dec 10, 2004
    #4
  5. HNguyen

    HNguyen Guest

    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:...
    > 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" <> wrote in message
    > news:eES$...
    > > 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:...
    > > > 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/default.asp?url=/library/en-us/vblr7/html/vastmSyncLock.asp
    > > >
    > > > Also, why reinvent the wheel?
    > > > http://logging.apache.org/log4net/
    > > >
    > > > Karl
    > > >
    > > > --
    > > > MY ASP.Net tutorials
    > > > http://www.openmymind.net/
    > > >
    > > >
    > > > "HNguyen" <> wrote in message
    > > > news:%23FU15%...
    > > > > 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.
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    HNguyen, Dec 21, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. clercmedia
    Replies:
    2
    Views:
    32,316
    clercmedia
    Dec 9, 2005
  2. lerenard001
    Replies:
    0
    Views:
    425
    lerenard001
    Jul 7, 2008
  3. lerenard001
    Replies:
    0
    Views:
    476
    lerenard001
    Jul 9, 2008
  4. id10t error
    Replies:
    3
    Views:
    733
    Alexey Smirnov
    Aug 31, 2009
  5. albert kao
    Replies:
    7
    Views:
    3,018
Loading...

Share This Page