Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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));
}                
share|improve this question
    
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); –  Anders Aune Oct 27 '12 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 Oct 29 '12 at 15:52

2 Answers 2

up vote 2 down vote accepted

Add the MemoryStream as a byte[]:

folder.Files.Add(fileName, memoryStream.ToArray(), bool);
share|improve this answer

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();
share|improve this answer
    
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 Oct 29 '12 at 15:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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