|
5 | 5 | using System.Collections.Generic; |
6 | 6 | using System.IO; |
7 | 7 | using System.Linq; |
| 8 | +using Microsoft.Azure.WebJobs.Host; |
8 | 9 | using Microsoft.Azure.WebJobs.Logging; |
9 | 10 | using Microsoft.Azure.WebJobs.Script.Diagnostics; |
10 | 11 | using Microsoft.Extensions.Logging; |
@@ -157,6 +158,62 @@ public void FileLogger_LogsExpectedLines() |
157 | 158 | t => Assert.EndsWith("[Information,TestFunction] Test Message With Function", t)); |
158 | 159 | } |
159 | 160 |
|
| 161 | + [Fact] |
| 162 | + public void FileLogger_LogsFunctionInvocationException() |
| 163 | + { |
| 164 | + var logger = new FileLogger(_category, _fileWriter, () => true, isPrimary: () => true, logType: LogType.Host); |
| 165 | + |
| 166 | + // throw an exception so we have a stack |
| 167 | + try |
| 168 | + { |
| 169 | + var innerEx = new InvalidOperationException("boom!"); |
| 170 | + var ex = new FunctionInvocationException("Error executing function.", innerEx); |
| 171 | + throw ex; |
| 172 | + } |
| 173 | + catch (Exception ex) |
| 174 | + { |
| 175 | + logger.LogError(ex, "Test Message"); |
| 176 | + } |
| 177 | + |
| 178 | + _fileWriter.Flush(); |
| 179 | + |
| 180 | + string logFile = Directory.EnumerateFiles(_logFilePath).Single(); |
| 181 | + string[] lines = File.ReadAllLines(logFile); |
| 182 | + |
| 183 | + // A FunctionInvocationException only logs the message. |
| 184 | + Assert.Collection(lines, |
| 185 | + t => Assert.EndsWith("[Error] Test Message", t), |
| 186 | + t => Assert.Equal("boom!", t)); |
| 187 | + } |
| 188 | + |
| 189 | + [Fact] |
| 190 | + public void FileLogger_LogsException() |
| 191 | + { |
| 192 | + var logger = new FileLogger(_category, _fileWriter, () => true, isPrimary: () => true, logType: LogType.Host); |
| 193 | + |
| 194 | + // throw an exception so we have a stack |
| 195 | + try |
| 196 | + { |
| 197 | + var ex = new InvalidOperationException("boom!"); |
| 198 | + throw ex; |
| 199 | + } |
| 200 | + catch (Exception ex) |
| 201 | + { |
| 202 | + logger.LogError(ex, "Test Message"); |
| 203 | + } |
| 204 | + |
| 205 | + _fileWriter.Flush(); |
| 206 | + |
| 207 | + string logFile = Directory.EnumerateFiles(_logFilePath).Single(); |
| 208 | + string[] lines = File.ReadAllLines(logFile); |
| 209 | + |
| 210 | + // This isn't a FunctionInvocationException, which means we'll see the stack. |
| 211 | + Assert.Collection(lines, |
| 212 | + t => Assert.EndsWith("[Error] Test Message", t), |
| 213 | + t => Assert.Equal("System.InvalidOperationException : boom!", t), |
| 214 | + t => Assert.Contains("at Microsoft.Azure.WebJobs.Script.Tests.Diagnostics.FileLoggerTests.FileLogger_LogsException()", t)); |
| 215 | + } |
| 216 | + |
160 | 217 | [Fact] |
161 | 218 | public void GetLogPrefix_ReturnsExpectedValue() |
162 | 219 | { |
|
0 commit comments