Working with files....
-
Well here I am again, heres what Im trying to to. I have a Folder Ive created in a specified path withint the application, there will be more than one file in said directory so heres what I need to do: This folder is used to hold the ErrLog.txt file I created in the application to log errors that have happened, I am now wanting to limit the size of this file to 10MB, so I need to be able to retrieve the current file being used (whether it be ErrLog1.txt, ErrLog2.txt, and so on) then check the size of this file and if its too big then close it, open a new file (with the next number appended to it) and use that file until it reaches the size limit, and so on. Ive done some searches on Google (as well as some other forums Im a member of) and havent found anything real useful, so I always return to the one place I can find answers, CP. Anyone got some ideas/links/samples I can look at and use to accomplish this?
"Let's face it, the average computer user has the brain of a Spider Monkey." Bill Gates
This is easy if you think about it. You don't need the log file open all the time. You open it, write your message, close it. Simple. Now, all you have to do is check the size of the file BEFORE your open the log and write to it. The FileInfo class comes in handy for that. If the size of the current logfile is greater than 10MB, increment the filename and use the new name for the log file.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
This is easy if you think about it. You don't need the log file open all the time. You open it, write your message, close it. Simple. Now, all you have to do is check the size of the file BEFORE your open the log and write to it. The FileInfo class comes in handy for that. If the size of the current logfile is greater than 10MB, increment the filename and use the new name for the log file.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007Well I've gotten that far but am having a few issues: Figuring out what is the file currently being used (as theoretically there could be multiple files after years of use), how to determine the number at the end of the file). I already have coded to determine the size of the file. I may be over-complicating this, sometimes I do that, but I seem to have hit a wall on this.
"Well yes, it is an Integer, but it's a metrosexual Integer. For all we know, under all that hair gel it could be a Boolean." Tom Welch
-
Well I've gotten that far but am having a few issues: Figuring out what is the file currently being used (as theoretically there could be multiple files after years of use), how to determine the number at the end of the file). I already have coded to determine the size of the file. I may be over-complicating this, sometimes I do that, but I seem to have hit a wall on this.
"Well yes, it is an Integer, but it's a metrosexual Integer. For all we know, under all that hair gel it could be a Boolean." Tom Welch
Psycho-*Coder*-Extreme wrote:
Figuring out what is the file currently being used
:confused: When you change the filename, you save it to either an XML config file you design, or your app.config file, or a spot in the registry.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
Psycho-*Coder*-Extreme wrote:
Figuring out what is the file currently being used
:confused: When you change the filename, you save it to either an XML config file you design, or your app.config file, or a spot in the registry.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007I came up with this solution:
Private Shared Function NeedNewFile(ByVal fileName As String) As Boolean
Dim filePath As String = sPathToUse & "\Errors\" & fileName
If File.Exists(filePath) Then
Dim fInfo As New FileInfo(filePath)
Dim fileSize As Long = fInfo.Length
With fInfo
If .Length >= (MAX_ERRORLOG_SIZE * 1024 * 1024) Then
File.Copy(filePath, filePath & "_Archive_" & Now.Day & Now.Month.ToString("MMM") & Now.Year & ".txt")
Dim sWriter As New StreamWriter(filePath, False)
sWriter.WriteLine("--New Log Created --//\\ " & Now())
sWriter.Close()
Return True
Else
Return False
End If
End With
End If
End FunctionAd this seems to do what I need it to do (so far), now I need to do some more testing, make the target path larger than 10MB and see what happens.
"Okay, I give up: which is NOT a real programming language????" Michael Bergman
-
Well I've gotten that far but am having a few issues: Figuring out what is the file currently being used (as theoretically there could be multiple files after years of use), how to determine the number at the end of the file). I already have coded to determine the size of the file. I may be over-complicating this, sometimes I do that, but I seem to have hit a wall on this.
"Well yes, it is an Integer, but it's a metrosexual Integer. For all we know, under all that hair gel it could be a Boolean." Tom Welch
Psycho-*Coder*-Extreme wrote:
there could be multiple files after years of use
Why are you anticipating this many errors during the lifetime of the app? I have absolutely no idea about what the requirements of your project are but that doesn't sound quite right to me.
-
Psycho-*Coder*-Extreme wrote:
there could be multiple files after years of use
Why are you anticipating this many errors during the lifetime of the app? I have absolutely no idea about what the requirements of your project are but that doesn't sound quite right to me.
Dave, I'm really not anticipating that many errors from the parts that I'm responsible for, but I work with a relatively new and young programmer who has a hard time (it seems) to do adequate exception handling. Realistically there may never be more than a couple error logs over the lifespan of the application, but I have a hard time allowing myself to assume something wont happen, I have to code for exceptions to the rules as well because you never know what the users going to do, thats just the way I am.
"Okay, I give up: which is NOT a real programming language????" Michael Bergman
-
Dave, I'm really not anticipating that many errors from the parts that I'm responsible for, but I work with a relatively new and young programmer who has a hard time (it seems) to do adequate exception handling. Realistically there may never be more than a couple error logs over the lifespan of the application, but I have a hard time allowing myself to assume something wont happen, I have to code for exceptions to the rules as well because you never know what the users going to do, thats just the way I am.
"Okay, I give up: which is NOT a real programming language????" Michael Bergman
Psycho-*Coder*-Extreme wrote:
who has a hard time (it seems) to do adequate exception handling
Slap him. Slap him silly.
-
Psycho-*Coder*-Extreme wrote:
who has a hard time (it seems) to do adequate exception handling
Slap him. Slap him silly.
Dave Sexton wrote:
Slap him. Slap him silly.
I have thought about this, but I know it will cost me my job (if I knew I could get away with it I would probably do it :->. Once, he actually "bitched" at me for putting code in a Try....Catch block, saying that an error couldn't possibly happen in that function and that I needed to remove it, to which I replied "Assumptions are the mother of all f**k ups". He has a bad habit of assuming something cant happen, or that the user cant possibly do something to raise an error. No matter how hard I've tried to explain this he just wont listen, but hey in the past 4 months he's had over 20 "trouble tickets" for the processes he's coded, and I've had 2. Maybe someday he'll grow out of his arrogance and start seeing things in the right light :~
"Okay, I give up: which is NOT a real programming language????" Michael Bergman
-
Dave Sexton wrote:
Slap him. Slap him silly.
I have thought about this, but I know it will cost me my job (if I knew I could get away with it I would probably do it :->. Once, he actually "bitched" at me for putting code in a Try....Catch block, saying that an error couldn't possibly happen in that function and that I needed to remove it, to which I replied "Assumptions are the mother of all f**k ups". He has a bad habit of assuming something cant happen, or that the user cant possibly do something to raise an error. No matter how hard I've tried to explain this he just wont listen, but hey in the past 4 months he's had over 20 "trouble tickets" for the processes he's coded, and I've had 2. Maybe someday he'll grow out of his arrogance and start seeing things in the right light :~
"Okay, I give up: which is NOT a real programming language????" Michael Bergman
Psycho-*Coder*-Extreme wrote:
"Assumptions are the mother of all f**k ups"
I remember a guy on a development team I was part of yelling that at one of the sales guys who demanded a feature that we later discovered no one wanted. The sales guy just assumed that a potential customer wanted a certain feature. He didn't. And he didn't buy either.
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website
-
Dave Sexton wrote:
Slap him. Slap him silly.
I have thought about this, but I know it will cost me my job (if I knew I could get away with it I would probably do it :->. Once, he actually "bitched" at me for putting code in a Try....Catch block, saying that an error couldn't possibly happen in that function and that I needed to remove it, to which I replied "Assumptions are the mother of all f**k ups". He has a bad habit of assuming something cant happen, or that the user cant possibly do something to raise an error. No matter how hard I've tried to explain this he just wont listen, but hey in the past 4 months he's had over 20 "trouble tickets" for the processes he's coded, and I've had 2. Maybe someday he'll grow out of his arrogance and start seeing things in the right light :~
"Okay, I give up: which is NOT a real programming language????" Michael Bergman
Psycho-*Coder*-Extreme wrote:
but hey in the past 4 months he's had over 20 "trouble tickets" for the processes he's coded, and I've had 2.
Is he assuming he won't get fired for his "quality" code? That's the mother of all f-ups. :-D
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
Psycho-*Coder*-Extreme wrote:
but hey in the past 4 months he's had over 20 "trouble tickets" for the processes he's coded, and I've had 2.
Is he assuming he won't get fired for his "quality" code? That's the mother of all f-ups. :-D
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007Dave Kreskowiak wrote:
Is he assuming he won't get fired for his "quality" code?
Dave, Thats exactly what he's assuming. He's been there coming up on 4 years, and we were both made part of the IT department about 4 months ago, we were just the personal programmers for one department, we didn't have to follow any of the SOCKS(sp?) Compliance issues, none of the Software Development Life Cycle or anything, if they wanted something we made it then gave it to them, so up until now no one ever knew of any "bugs" he wrote. I'm hoping that now that they're coming to light something will be done about it.
"Okay, I give up: which is NOT a real programming language????" Michael Bergman