Ejemplo n.º 1
0
        async Task WriteEntryAsync(LogFileInfo logFile, FileLogEntry entry, CancellationToken disposeToken)
        {
            // discarding remaining entries if queue got disposed
            disposeToken.ThrowIfCancellationRequested();

            Encoding fileEncoding;
            string   filePath;
            bool     ensureBasePath;

            _settingsLock.EnterReadLock();
            try
            {
                fileEncoding = GetFileEncoding(logFile, entry);
                var postfix = GetPostfix(logFile, fileEncoding, entry);
                filePath       = logFile.GetFilePath(postfix);
                ensureBasePath = Settings.EnsureBasePath;
            }
            finally { _settingsLock.ExitReadLock(); }

            var fileInfo = Context.FileProvider.GetFileInfo(filePath);

            while (true)
            {
                try
                {
                    await Context.AppendAllTextAsync(fileInfo, entry.Text, fileEncoding).ConfigureAwait(false);

                    return;
                }
                catch
                {
                    if (ensureBasePath)
                    {
                        try
                        {
                            if (await Context.EnsureDirAsync(fileInfo).ConfigureAwait(false))
                            {
                                await Context.AppendAllTextAsync(fileInfo, entry.Text, fileEncoding).ConfigureAwait(false);

                                return;
                            }
                        }
                        catch { }
                    }
                }

                // discarding failed entry if queue got disposed
                await Task.Delay(1000, disposeToken).ConfigureAwait(false);
            }
        }
Ejemplo n.º 2
0
        async Task WriteEntryAsync(LogFileInfo logFile, FileLogEntry entry, CancellationToken shutdownToken)
        {
            // discarding remaining entries on shutdown
            shutdownToken.ThrowIfCancellationRequested();

            var fileAppender   = GetFileAppender(logFile);
            var fileEncoding   = GetFileEncoding(logFile, entry);
            var postfix        = GetPostfix(logFile, fileAppender, fileEncoding, entry);
            var filePath       = logFile.GetFilePath(postfix);
            var ensureBasePath = logFile.Settings.EnsureBasePath;

            var fileInfo = fileAppender.FileProvider.GetFileInfo(filePath);

            while (true)
            {
                try
                {
                    await fileAppender.AppendAllTextAsync(fileInfo, entry.Text, fileEncoding, shutdownToken).ConfigureAwait(false);

                    return;
                }
                catch
                {
                    if (ensureBasePath)
                    {
                        try
                        {
                            if (await fileAppender.EnsureDirAsync(fileInfo, shutdownToken).ConfigureAwait(false))
                            {
                                await fileAppender.AppendAllTextAsync(fileInfo, entry.Text, fileEncoding, shutdownToken).ConfigureAwait(false);

                                return;
                            }
                        }
                        catch { }
                    }
                }

                // discarding failed entry on shutdown
                if (Context.WriteRetryDelay > TimeSpan.Zero)
                {
                    await Task.Delay(Context.WriteRetryDelay, shutdownToken).ConfigureAwait(false);
                }
                else
                {
                    shutdownToken.ThrowIfCancellationRequested();
                }
            }
        }
Ejemplo n.º 3
0
        protected virtual bool CheckLogFile(LogFileInfo logFile, string postfix, IFileAppender fileAppender, Encoding fileEncoding, FileLogEntry entry)
        {
            if (logFile.Settings.MaxFileSize > 0)
            {
                var fileInfo = fileAppender.FileProvider.GetFileInfo(logFile.GetFilePath(postfix));
                if (fileInfo.Exists &&
                    (fileInfo.IsDirectory || fileInfo.Length + fileEncoding.GetByteCount(entry.Text) > logFile.Settings.MaxFileSize))
                {
                    logFile.Counter++;
                    return(false);
                }
            }

            return(true);
        }