|
1 |
| -// Copyright (c) .NET Foundation. All rights reserved. |
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
2 | 2 | // Licensed under the MIT License. See License.txt in the project root for license information.
|
3 | 3 |
|
4 | 4 | using System;
|
5 | 5 | using System.Collections.Generic;
|
6 | 6 | using System.Linq;
|
7 |
| -using System.Threading.Tasks; |
| 7 | +using System.Text; |
8 | 8 | using Microsoft.Azure.WebJobs.Host;
|
9 | 9 | using Microsoft.Azure.WebJobs.Host.Listeners;
|
10 | 10 | using Microsoft.Azure.WebJobs.Logging;
|
11 | 11 | using Microsoft.Azure.WebJobs.Script.Configuration;
|
12 | 12 | using Microsoft.Azure.WebJobs.Script.WebHost;
|
13 | 13 | using Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics;
|
| 14 | +using Microsoft.Azure.WebJobs.Script.WebHost.Security; |
| 15 | +using Microsoft.Azure.WebJobs.Script.Workers.Rpc; |
14 | 16 | using Microsoft.Extensions.DependencyInjection;
|
15 | 17 | using Microsoft.Extensions.Hosting;
|
16 | 18 | using Microsoft.Extensions.Logging;
|
@@ -310,6 +312,50 @@ public void AppEnvironment_Reset_OnSpecialization()
|
310 | 312 | Assert.Equal("updatedruntimesitename", evt.RuntimeSiteName);
|
311 | 313 | }
|
312 | 314 |
|
| 315 | + [Fact] |
| 316 | + public void Log_RpcException() |
| 317 | + { |
| 318 | + string secretString = "{ \"AzureWebJobsStorage\": \"DefaultEndpointsProtocol=https;AccountName=testAccount1;AccountKey=mykey1;EndpointSuffix=core.windows.net\", \"AnotherKey\": \"AnotherValue\" }"; |
| 319 | + var innerException = new RpcException("result", secretString, "stack", "type"); |
| 320 | + var functionInvocationException = new FunctionInvocationException("Invocation failed", Guid.Empty, "Functions.TestFunction", innerException); |
| 321 | + var formattedMessage = "Test log"; |
| 322 | + var hash = EncryptionHelper.GetSHA256Base64String(Encoding.UTF8.GetBytes(innerException.RemoteMessage)); |
| 323 | + var innerExceptionType = innerException.GetType().ToString(); |
| 324 | + var eventName = string.Empty; |
| 325 | + var functionInvocationId = string.Empty; |
| 326 | + var activityId = string.Empty; |
| 327 | + |
| 328 | + _mockEventGenerator.Setup(p => p.LogFunctionTraceEvent(LogLevel.Error, _subscriptionId, _websiteName, _functionName, eventName, _category, It.Is<string>(s => s.Contains(hash)), |
| 329 | + formattedMessage, innerExceptionType, It.Is<string>(s => s.Contains(hash)), functionInvocationId, _hostInstanceId, activityId, _runtimeSiteName, _slotName, It.IsAny<DateTime>())); |
| 330 | + |
| 331 | + _logger.LogError(functionInvocationException, formattedMessage); |
| 332 | + |
| 333 | + _mockEventGenerator.VerifyAll(); |
| 334 | + } |
| 335 | + |
| 336 | + [Fact] |
| 337 | + public void Log_NonRpcException() |
| 338 | + { |
| 339 | + var secretReplacement = "[Hidden Credential]"; |
| 340 | + var secretString = "{ \"AzureWebJobsStorage\": \"DefaultEndpointsProtocol=https;AccountName=testAccount1;AccountKey=mykey1;EndpointSuffix=core.windows.net\", \"AnotherKey\": \"AnotherValue\" }"; |
| 341 | + var sanitizedString = $"{{ \"AzureWebJobsStorage\": \"{secretReplacement}\", \"AnotherKey\": \"AnotherValue\" }}"; |
| 342 | + |
| 343 | + var sanitizedDetails = "System.ArgumentNullException : Value cannot be null. (Parameter 'result')"; |
| 344 | + var sanitizedExceptionMessage = "Value cannot be null. (Parameter 'result')"; |
| 345 | + |
| 346 | + var eventName = string.Empty; |
| 347 | + var functionInvocationId = string.Empty; |
| 348 | + var activityId = string.Empty; |
| 349 | + |
| 350 | + var ex = new ArgumentNullException("result"); |
| 351 | + |
| 352 | + _mockEventGenerator.Setup(p => p.LogFunctionTraceEvent(LogLevel.Error, _subscriptionId, _websiteName, _functionName, eventName, _category, sanitizedDetails, sanitizedString, ex.GetType().ToString(), sanitizedExceptionMessage, functionInvocationId, _hostInstanceId, activityId, _runtimeSiteName, _slotName, It.IsAny<DateTime>())); |
| 353 | + |
| 354 | + _logger.LogError(ex, Sanitizer.Sanitize(secretString)); |
| 355 | + |
| 356 | + _mockEventGenerator.VerifyAll(); |
| 357 | + } |
| 358 | + |
313 | 359 | public class FunctionExceptionDataProvider
|
314 | 360 | {
|
315 | 361 | public static IEnumerable<object[]> TestCases
|
|
0 commit comments