1

Here is the setup. On error i want to write (append) the error message to an SPFile in a document library. If the file doesn't already exist i want to create it. Searching around this is what i came up with. However, when it attempts to create the file using SPFolder.Files.Add it throws an I/O Error exception (no inner exception/better description during debug). If the file is already there, however, all is well and the file is updated as expected. So first question would be what am i doing wrong. Second would be is there a better way to do what i am doing (no doubt there is).....any help appreciated

System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
UnicodeEncoding uniEncoding = new UnicodeEncoding();
StringBuilder errors = new StringBuilder();

foreach (Exception e in cc.aggregatedException.m_InnerExceptionsList)
{
    errors.Append(e.Message + Environment.NewLine);
}

SPFile logFile = csvList.Items.Cast<SPListItem>()
                              .Where(x => x.Name.Equals(logFileName))
                              .Select(x => x.File)
                              .FirstOrDefault();

if (logFile == null)
{
    using (MemoryStream ms = new MemoryStream())
    {
         using (StreamWriter sw = new StreamWriter(ms, uniEncoding))
         {
              sw.Write(errors.ToString());
         }

         SPFolder csvLibraryFolder = SPContext.Current.Web.Folders[libraryName];
         csvLibraryFolder.Files.Add(logFileName, ms, false);
    }
}
else
{
    byte[] fileContents = logFile.OpenBinary();
    string newContents = enc.GetString(fileContents) + 
                             Environment.NewLine + errors.ToString();
    logFile.SaveBinary(enc.GetBytes(newContents));
}                
2
  • Can't say that I see any errors. Not that often I add files with MemoryStream, but I have always added them like this: folder.Files.Add(fileName, memoryStream.ToArray(), bool); Commented Oct 27, 2012 at 11:12
  • That fixed it....if you want add this as an answer and i'll give you the credit. Can anyone tell me why passing a byte[] vs just the memorystream to the Add method causes an I/O Exception? Many many thanks!!!!
    – MikeC
    Commented Oct 29, 2012 at 15:52

2 Answers 2

2

Add the MemoryStream as a byte[]:

folder.Files.Add(fileName, memoryStream.ToArray(), bool);
2

This is a simple approach I have used in the past with a try/catch so maybe you could revamp for your needs:

string myResponse = "whatever needs to be written to the file...";
        string filename = "C:\\splogs\\" + (DateTime.Now).ToString("yyyyMMdd") + "_customlog.log";
        FileStream logStream = new FileStream(@filename, FileMode.Append);
        StreamWriter logfile = new StreamWriter(logStream);
        logfile.WriteLine(myResponse.ToString());
        logfile.Close();
1
  • A good approach to log to a file in the file system. +1 :) I would prefer to log to a file in the actual document library because otherwise nobody will ever pay attention to it (or more importantly have the access to pay attention to it :)
    – MikeC
    Commented Oct 29, 2012 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.