Skip to content

Commit 23fd3f1

Browse files
committed
Apply default source only when not specified
1 parent e010aba commit 23fd3f1

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/WebJobs.Script/Extensions/TraceWriterExtensions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ public static TraceWriter Apply(this TraceWriter traceWriter, IDictionary<string
3636
return new InterceptingTraceWriter(traceWriter, interceptor);
3737
}
3838

39-
public static TraceWriter WithSource(this TraceWriter traceWriter, string source) => new InterceptingTraceWriter(traceWriter, t => t.Source = source);
39+
/// <summary>
40+
/// Apply the specified default source value to all trace events
41+
/// when the source is not specified.
42+
/// </summary>
43+
public static TraceWriter WithSource(this TraceWriter traceWriter, string source) => new InterceptingTraceWriter(traceWriter, t => t.Source = string.IsNullOrEmpty(t.Source) ? source : t.Source);
4044

4145
public static void Verbose(this TraceWriter traceWriter, string message, IDictionary<string, object> properties)
4246
{

test/WebJobs.Script.Tests.Integration/SamplesEndToEndTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,8 @@ public async Task HostLog_Anonymous_Fails()
10151015
[Fact]
10161016
public async Task HostLog_AdminLevel_Succeeds()
10171017
{
1018+
TestHelpers.ClearHostLogs();
1019+
10181020
var request = new HttpRequestMessage(HttpMethod.Post, "admin/host/log");
10191021
request.Headers.Add(AuthorizationLevelAttribute.FunctionsKeyHeaderName, MasterKey);
10201022
var logs = new HostLogEntry[]

test/WebJobs.Script.Tests/Diagnostics/TraceWriterExtensionsTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@ namespace Microsoft.Azure.WebJobs.Script.Tests
1212
{
1313
public class TraceWriterExtensionsTests
1414
{
15+
[Fact]
16+
public void WithSource_AppliesDefaultSource()
17+
{
18+
TestTraceWriter testTraceWriter = new TestTraceWriter(TraceLevel.Verbose);
19+
var wrappedTraceWriter = testTraceWriter.WithSource("TestDefault");
20+
21+
wrappedTraceWriter.Info("Test1");
22+
wrappedTraceWriter.Info("Test2", "CustomSource");
23+
wrappedTraceWriter.Info("Test3", string.Empty);
24+
25+
Assert.Equal(3, testTraceWriter.Traces.Count);
26+
27+
Assert.Equal("Test1", testTraceWriter.Traces[0].Message);
28+
Assert.Equal("TestDefault", testTraceWriter.Traces[0].Source);
29+
30+
Assert.Equal("Test2", testTraceWriter.Traces[1].Message);
31+
Assert.Equal("CustomSource", testTraceWriter.Traces[1].Source);
32+
33+
Assert.Equal("Test3", testTraceWriter.Traces[2].Message);
34+
Assert.Equal("TestDefault", testTraceWriter.Traces[2].Source);
35+
}
36+
1537
[Fact]
1638
public void Apply_CreatesInterceptingTraceWriter()
1739
{

0 commit comments

Comments
 (0)