Example #1
0
        public async Task <bool> WriteLogEntryAsync(string entry)
        {
            var newFileName = _options.FileName();

            // if not been initialized for 1st time, then do so
            if (!_initialized)
            {
                await InitializeAsync();
            }

            // file name change implies we need to create / open a log file...
            if (FileNameChanged(newFileName))
            {
                await OpenLogFileAsync(newFileName, _options.ClearAtStart).ConfigureAwait(false);
            }

            using (var stream = await _currentLogFile.OpenAsync(FileAccess.ReadAndWrite).ConfigureAwait(false))
            {
                // now want to append... the formatted string...
                stream.Seek(0, SeekOrigin.End);

                var formattedEntry = entry + "\r\n";

                var data = Encoding.UTF8.GetBytes(formattedEntry);
                await stream.WriteAsync(data, 0, data.Length);
            }

            return(true);
        }