Some background... I am not a VB coder... I know enough that I can get by for simple things, but usually only after looking at examples online. But I occasionally get tasked with stuff that involves some light coding.
In this case, we have a file that we send to our parent company. I wrote a program that does the following things:
1. Run an Reporting Services report and dump the results to a text file
2. FTP that text file to the company
3. Attach that same text file to an email
4. Attach the log of the program running to the same email
5. Send the email
The problem is related to step four... trying to attach the log file while the program is still running. The log is written out by a sub that opens the file, writes a line, then immediately closes the file. It worked fine for almost three years, and then last week we started getting failures. Ran the program manually and it turned out that it was failing to attach the log to the email, and even though I was using a Try-Catch, the program was crashing on that attempt.
A new log file is created every time, so it wasn't being locked by any other process.
While tinkering with the thing, I added a write to the console to show the file name - the second "Console.Writeline" in the code below - just to double check that it was getting the name right. Lo and behold, it worked. Took the new line out, and it failed again. Put it back in and it works.
How does sending the name of the log file to console fix the issue of it failing to attach the log file to an email?
Console.WriteLine("Attaching Logfile to email.")
DateAndTime.Now.ToString("yyyy_MM_dd") + ".log")
e_mail.Attachments.Add(New Attachment("\\utilserver\c$\DAILY_report\log\DAILY_" +
DateAndTime.Now.ToString("yyyy_MM_dd") + ".log"))
Catch ex As Exception
Console.WriteLine("Failed to attach log to email: " + ex.Message)
LogMessage("Failed to attach log to the email: " + ex.Message)