| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | using System.Text; |
| | | 4 | | |
| | | 5 | | namespace AmbientServices; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Default <see cref="IAmbientLogOverflowWriter"/> that appends overflow lines to a file under local application data |
| | | 9 | | /// (same folder convention as <see cref="AmbientFileLogger"/>). |
| | | 10 | | /// Uses a single long-lived <see cref="StreamWriter"/> per instance to avoid recursion through ambient loggers and per- |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// <pitch>The default place overflowed log lines land: an append-only file under local application data, so a logging b |
| | | 14 | | /// <pledge><see cref="IAmbientLogOverflowWriter"/></pledge> |
| | | 15 | | /// <pledge><see cref="IDisposable"/></pledge> |
| | | 16 | | /// <pledge> |
| | | 17 | | /// All lines from one instance are appended to a single fixed file — the path given at construction, or the default ove |
| | | 18 | | /// Flushing (or disposing) closes the open writer so the file can be read externally; a later write transparently reope |
| | | 19 | | /// </pledge> |
| | | 20 | | /// <plan> |
| | | 21 | | /// A single lazily-created <see cref="StreamWriter"/> (UTF-8, auto-flush) over a <see cref="FileStream"/> opened with < |
| | | 22 | | /// Trade-off profile: durable and simple at the cost of a lock and a synchronous write per line; acceptable because ove |
| | | 23 | | /// </plan> |
| | | 24 | | /// </remarks> |
| | | 25 | | [DefaultAmbientService(typeof(IAmbientLogOverflowWriter))] |
| | | 26 | | public sealed class AmbientFileLogOverflowWriter : IAmbientLogOverflowWriter, IDisposable |
| | | 27 | | { |
| | 2 | 28 | | private readonly object _writeLock = new(); |
| | | 29 | | private readonly string _overflowLogFilePath; |
| | | 30 | | private StreamWriter? _writer; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Constructs a writer that uses the standard local application data overflow log path. |
| | | 34 | | /// </summary> |
| | | 35 | | public AmbientFileLogOverflowWriter() |
| | 2 | 36 | | : this(DefaultOverflowLogFilePath) |
| | | 37 | | { |
| | 2 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Constructs a writer that appends to the specified file path. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="overflowLogFilePath">The full path of the overflow log file.</param> |
| | 2 | 44 | | public AmbientFileLogOverflowWriter(string overflowLogFilePath) |
| | | 45 | | { |
| | | 46 | | #if NET5_0_OR_GREATER |
| | 2 | 47 | | ArgumentNullException.ThrowIfNull(overflowLogFilePath); |
| | | 48 | | #else |
| | | 49 | | if (overflowLogFilePath is null) throw new ArgumentNullException(nameof(overflowLogFilePath)); |
| | | 50 | | #endif |
| | 2 | 51 | | _overflowLogFilePath = overflowLogFilePath; |
| | 2 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets the default overflow log file path (same folder convention as <see cref="AmbientFileLogger"/>). |
| | | 56 | | /// </summary> |
| | | 57 | | public static string DefaultOverflowLogFilePath |
| | | 58 | | { |
| | | 59 | | get |
| | | 60 | | { |
| | 2 | 61 | | string prefix = AmbientFileLogger.CombineRelativeFilePrefixWithProgramData( |
| | 2 | 62 | | AmbientFileLogger.GetExecutableName() + "_AmbientLogBufferOverflow", |
| | 2 | 63 | | AmbientFileLogger.GetProgramDataFolderLocationInternal, |
| | 2 | 64 | | AmbientFileLogger.GetExecutableName); |
| | 2 | 65 | | return prefix + ".log"; |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <inheritdoc /> |
| | | 70 | | public void WriteOverflowLine(string line) |
| | | 71 | | { |
| | 2 | 72 | | if (line == null) return; |
| | | 73 | | try |
| | | 74 | | { |
| | 2 | 75 | | lock (_writeLock) |
| | | 76 | | { |
| | 2 | 77 | | GetOrCreateWriter().WriteLine(line); |
| | 2 | 78 | | } |
| | 2 | 79 | | } |
| | | 80 | | #pragma warning disable CA1031 |
| | 0 | 81 | | catch |
| | | 82 | | #pragma warning restore CA1031 |
| | | 83 | | { |
| | 0 | 84 | | } |
| | 2 | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <inheritdoc /> |
| | | 88 | | public void Flush() |
| | | 89 | | { |
| | 2 | 90 | | lock (_writeLock) |
| | | 91 | | { |
| | 2 | 92 | | CloseWriter(); |
| | 2 | 93 | | } |
| | 2 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <inheritdoc /> |
| | 2 | 97 | | public void Dispose() => Flush(); |
| | | 98 | | |
| | | 99 | | private StreamWriter GetOrCreateWriter() |
| | | 100 | | { |
| | 2 | 101 | | if (_writer != null) |
| | | 102 | | { |
| | 2 | 103 | | return _writer; |
| | | 104 | | } |
| | | 105 | | |
| | 2 | 106 | | string? directory = Path.GetDirectoryName(_overflowLogFilePath); |
| | 2 | 107 | | if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) |
| | | 108 | | { |
| | 2 | 109 | | Directory.CreateDirectory(directory); |
| | | 110 | | } |
| | | 111 | | |
| | 2 | 112 | | FileStream stream = new(_overflowLogFilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); |
| | 2 | 113 | | _writer = new StreamWriter(stream, Encoding.UTF8) { AutoFlush = true }; |
| | 2 | 114 | | return _writer; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | private void CloseWriter() |
| | | 118 | | { |
| | 2 | 119 | | if (_writer == null) |
| | | 120 | | { |
| | 2 | 121 | | return; |
| | | 122 | | } |
| | | 123 | | |
| | 2 | 124 | | StreamWriter writer = _writer; |
| | 2 | 125 | | _writer = null; |
| | | 126 | | try |
| | | 127 | | { |
| | 2 | 128 | | writer.Flush(); |
| | 2 | 129 | | writer.Dispose(); |
| | 2 | 130 | | } |
| | | 131 | | #pragma warning disable CA1031 |
| | 2 | 132 | | catch |
| | | 133 | | #pragma warning restore CA1031 |
| | | 134 | | { |
| | 2 | 135 | | } |
| | 2 | 136 | | } |
| | | 137 | | } |