/// <summary>
        /// Initialize the temp folder with every attachments copies
        /// </summary>
        public void CreateAttachmentsCopies()
        {
            IList <Log>   logs        = repo.GetAll();
            IMvxFileStore fileStore   = Mvx.Resolve <IMvxFileStore>();
            string        tempDirPath = @"./TempAttachments";

            // Create/Clear temp dir
            if (fileStore.FolderExists(tempDirPath))
            {
                fileStore.DeleteFolder(tempDirPath, true);
            }
            fileStore.EnsureFolderExists(tempDirPath);


            foreach (Log log in logs)
            {
                IList <string> attachmentsNames = repo.GetAttachmentNames(log.Id);
                if (attachmentsNames.Count > 0)
                {
                    fileStore.EnsureFolderExists(tempDirPath + "/" + log.Id);
                }
                foreach (string attachmentName in attachmentsNames)
                {
                    Stream stream = repo.GetAttachment(log.Id, attachmentName);
                    stream.Seek(0, SeekOrigin.Begin);
                    StreamReader sr = new StreamReader(stream);
                    //fileStore.WriteFile(tempDirPath + "/" + log.Id + "/" + attachmentName, sr.ReadToEnd());
                    int    b        = stream.ReadByte();
                    Stream tempFile = fileStore.OpenWrite(tempDirPath + "/" + log.Id + "/" + attachmentName);
                    while (b != -1)
                    {
                        tempFile.WriteByte((byte)b);
                        b = stream.ReadByte();
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void Write(string message)
        {
            try
            {
                var messageWithUserName = message + GetMessageBase();

                lock (_threadLock)
                {
                    if (_isWriting)
                    {
                        _waitingQueue.Add(messageWithUserName);
                        return;
                    }

                    _isWriting = true;

                    _waitingQueue.Add(messageWithUserName);

                    byte[] fileContentBytes = new byte[0];

                    if (_fileStoreService.Exists(GetLogFileName()))
                    {
                        _fileStoreService.TryReadBinaryFile(GetLogFileName(), out fileContentBytes);
                    }

                    if (_fileStoreService.Exists(GetLogFileName()) && fileContentBytes.Length + messageWithUserName.Length > LogFileMaximumSize)
                    {
                        #if DEBUG
                        Debug.WriteLine("**********************  Log is too long, removing older entries.");
                                                #endif
                        DeleteOldEntries(GetLogFileName(), (int)(fileContentBytes.Length + messageWithUserName.Length - LogFileMaximumSize));
                    }

                    var stream       = _fileStoreService.OpenWrite(GetLogFileName());
                    var streamWriter = new StreamWriter(stream);

                    while (_waitingQueue.Any())
                    {
                        var additionalLine = _waitingQueue.FirstOrDefault();

                        _waitingQueue.RemoveAt(0);

                        Debug.WriteLine(additionalLine);
                        streamWriter.WriteLine(additionalLine);
                    }

                    streamWriter.Flush();

                    // Forcing dispose of file handle to attempt to reduce the possibility of "Too many files opened".
                    streamWriter.Dispose();
                }
            }
            catch (Exception ex)
            {
                LogError(ex);
            }
            finally
            {
                GC.Collect();
                _isWriting = false;
            }
        }