|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Concurrent; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.IO; |
| 8 | +using System.Linq; |
| 9 | +using System.Threading; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +#if !FUNCTIONS_V1 |
| 13 | +using Mono.Unix.Native; |
| 14 | +#endif |
| 15 | + |
| 16 | +namespace Microsoft.Azure.WebJobs.Extensions.DurableTask |
| 17 | +{ |
| 18 | + /// <summary> |
| 19 | + /// The File logger for linux dedicated. Manages file rolling and is concurrency-safe. |
| 20 | + /// This is copied over from the azure-funtions-host codebase here: |
| 21 | + /// https://github.com/Azure/azure-functions-host/blob/35cf323fa3464a08b410a518bcab006e801301fe/src/WebJobs.Script.WebHost/Diagnostics/LinuxAppServiceFileLogger.cs |
| 22 | + /// We have modified their implementation to utilize syscall.rename instead of File.Move during file rolling. |
| 23 | + /// This change is necessary for older versions of fluent-bit, our logging infrastructure in linux dedicated, to properly deal with logfile archiving. |
| 24 | + /// </summary> |
| 25 | + public class LinuxAppServiceFileLogger |
| 26 | + { |
| 27 | + private readonly string logFileName; |
| 28 | + private readonly string logFileDirectory; |
| 29 | + private readonly string logFilePath; |
| 30 | + private readonly string archiveFilePath; |
| 31 | + private readonly BlockingCollection<string> buffer; |
| 32 | + private readonly List<string> currentBatch; |
| 33 | + private readonly CancellationTokenSource cancellationTokenSource; |
| 34 | + private Task outputTask; |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// The File logger for linux dedicated. Manages file rolling and is concurrency-safe. |
| 38 | + /// </summary> |
| 39 | + /// <param name="logFileName">Name of target logfile.</param> |
| 40 | + /// <param name="logFileDirectory">Directory of target logfile.</param> |
| 41 | + /// <param name="startOnCreate">Whether or not to start monitoring the write buffer at initialization time.</param> |
| 42 | + public LinuxAppServiceFileLogger(string logFileName, string logFileDirectory, bool startOnCreate = true) |
| 43 | + { |
| 44 | + this.logFileName = logFileName; |
| 45 | + this.logFileDirectory = logFileDirectory; |
| 46 | + this.logFilePath = Path.Combine(this.logFileDirectory, this.logFileName); |
| 47 | + this.archiveFilePath = this.logFilePath + "1"; |
| 48 | + this.buffer = new BlockingCollection<string>(new ConcurrentQueue<string>()); |
| 49 | + this.currentBatch = new List<string>(); |
| 50 | + this.cancellationTokenSource = new CancellationTokenSource(); |
| 51 | + |
| 52 | + if (startOnCreate) |
| 53 | + { |
| 54 | + this.Start(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Maximum size of individual log file in MB |
| 59 | + private int MaxFileSizeMb { get; set; } = 10; |
| 60 | + |
| 61 | + // Maximum time between successive flushes (seconds) |
| 62 | + private int FlushFrequencySeconds { get; set; } = 30; |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Log a string. |
| 66 | + /// </summary> |
| 67 | + /// <param name="message">Message to log.</param> |
| 68 | + public virtual void Log(string message) |
| 69 | + { |
| 70 | + try |
| 71 | + { |
| 72 | + this.buffer.Add(message); |
| 73 | + } |
| 74 | + catch (Exception) |
| 75 | + { |
| 76 | + // ignored |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private void Start() |
| 81 | + { |
| 82 | + if (this.outputTask == null) |
| 83 | + { |
| 84 | + this.outputTask = Task.Factory.StartNew(this.ProcessLogQueue, null, TaskCreationOptions.LongRunning); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Flushes the write buffer, stops writing to logfile afterwards. |
| 90 | + /// </summary> |
| 91 | + /// <param name="timeSpan">Timeout in milliseconds for flushing task.</param> |
| 92 | + public void Stop(TimeSpan timeSpan) |
| 93 | + { |
| 94 | + this.cancellationTokenSource.Cancel(); |
| 95 | + |
| 96 | + try |
| 97 | + { |
| 98 | + this.outputTask?.Wait(timeSpan); |
| 99 | + } |
| 100 | + catch (Exception) |
| 101 | + { |
| 102 | + // ignored |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private async Task ProcessLogQueue(object state) |
| 107 | + { |
| 108 | + while (!this.cancellationTokenSource.IsCancellationRequested) |
| 109 | + { |
| 110 | + await this.InternalProcessLogQueue(); |
| 111 | + await Task.Delay(TimeSpan.FromSeconds(this.FlushFrequencySeconds), this.cancellationTokenSource.Token).ContinueWith(task => { }); |
| 112 | + } |
| 113 | + |
| 114 | + await this.InternalProcessLogQueue(); |
| 115 | + |
| 116 | + // ReSharper disable once FunctionNeverReturns |
| 117 | + } |
| 118 | + |
| 119 | + // internal for unittests (in func host) |
| 120 | + internal async Task InternalProcessLogQueue() |
| 121 | + { |
| 122 | + string currentMessage; |
| 123 | + while (this.buffer.TryTake(out currentMessage)) |
| 124 | + { |
| 125 | + this.currentBatch.Add(currentMessage); |
| 126 | + } |
| 127 | + |
| 128 | + if (this.currentBatch.Any()) |
| 129 | + { |
| 130 | + try |
| 131 | + { |
| 132 | + await this.WriteLogs(this.currentBatch); |
| 133 | + } |
| 134 | + catch (Exception) |
| 135 | + { |
| 136 | + // Ignored |
| 137 | + } |
| 138 | + |
| 139 | + this.currentBatch.Clear(); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private async Task WriteLogs(IEnumerable<string> currentBatch) |
| 144 | + { |
| 145 | + // If the directory already exists, this does nothing |
| 146 | + Directory.CreateDirectory(this.logFileDirectory); |
| 147 | + |
| 148 | + var fileInfo = new FileInfo(this.logFilePath); |
| 149 | + if (fileInfo.Exists) |
| 150 | + { |
| 151 | + if (fileInfo.Length / (1024 * 1024) >= this.MaxFileSizeMb) |
| 152 | + { |
| 153 | + this.RollFiles(); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + await this.AppendLogs(this.logFilePath, currentBatch); |
| 158 | + } |
| 159 | + |
| 160 | + private async Task AppendLogs(string filePath, IEnumerable<string> logs) |
| 161 | + { |
| 162 | + using (var streamWriter = File.AppendText(filePath)) |
| 163 | + { |
| 164 | + foreach (var log in logs) |
| 165 | + { |
| 166 | + await streamWriter.WriteLineAsync(log); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + private void RollFiles() |
| 172 | + { |
| 173 | + // Rename current file to older file. |
| 174 | + |
| 175 | +#if !FUNCTIONS_V1 |
| 176 | + Syscall.rename(this.logFilePath, this.archiveFilePath); |
| 177 | +#endif |
| 178 | + |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments