-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for Missing Logs in ASP.NET Core 6.0 Web and Razor Apps (#2975)
* Fix missing logs issue * Update Changelog * Add tests. * PR feedback * Remove accidental merges. --------- Co-authored-by: Paulo Janotti <pjanotti@splunk.com>
- Loading branch information
1 parent
1538fcb
commit 1c6319a
Showing
9 changed files
with
221 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// <copyright file="LogRazorTests.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
#if NET6_0_OR_GREATER | ||
using IntegrationTests.Helpers; | ||
using Xunit.Abstractions; | ||
|
||
namespace IntegrationTests; | ||
|
||
public class LogRazorTests : TestHelper | ||
{ | ||
public LogRazorTests(ITestOutputHelper output) | ||
: base("Razor", output) | ||
{ | ||
} | ||
|
||
[Theory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
[Trait("Category", "EndToEnd")] | ||
public void SubmitLogs(bool enableClrProfiler) | ||
{ | ||
using var collector = new MockLogsCollector(Output); | ||
SetExporter(collector); | ||
collector.Expect(logRecord => | ||
{ | ||
var logsAsString = Convert.ToString(logRecord); | ||
return logsAsString != null && logsAsString.Contains("Warning from Razor App."); | ||
}); | ||
|
||
if (enableClrProfiler) | ||
{ | ||
EnableBytecodeInstrumentation(); | ||
} | ||
else | ||
{ | ||
SetEnvironmentVariable("ASPNETCORE_HOSTINGSTARTUPASSEMBLIES", "OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper"); | ||
} | ||
|
||
SetEnvironmentVariable("OTEL_DOTNET_AUTO_LOGS_INCLUDE_FORMATTED_MESSAGE", "true"); | ||
RunTestApplication(); | ||
|
||
collector.AssertExpectations(); | ||
} | ||
} | ||
#endif |
5 changes: 5 additions & 0 deletions
5
test/test-applications/integrations/TestApplication.Razor/Pages/Index.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@page | ||
<h1>Hello World!</h1> | ||
@model TestApplication.Razor.Pages.IndexModel | ||
@{ | ||
} |
36 changes: 36 additions & 0 deletions
36
test/test-applications/integrations/TestApplication.Razor/Pages/Index.cshtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// <copyright file="Index.cshtml.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace TestApplication.Razor.Pages; | ||
|
||
#pragma warning disable SA1649 // File name should match first type name | ||
public class IndexModel : PageModel | ||
#pragma warning restore SA1649 // File name should match first type name | ||
{ | ||
private readonly ILogger<IndexModel> _logger; | ||
|
||
public IndexModel(ILogger<IndexModel> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public void OnGet() | ||
{ | ||
_logger.LogWarning("Warning from Razor App."); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
test/test-applications/integrations/TestApplication.Razor/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// <copyright file="Program.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System.Net.Http; | ||
using Microsoft.AspNetCore.Hosting.Server; | ||
using Microsoft.AspNetCore.Hosting.Server.Features; | ||
|
||
namespace TestApplication.Razor; | ||
|
||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
using var host = CreateHostBuilder(args).Build(); | ||
host.Start(); | ||
|
||
var server = (IServer?)host.Services.GetService(typeof(IServer)); | ||
var addressFeature = server?.Features.Get<IServerAddressesFeature>(); | ||
var address = addressFeature?.Addresses.First(); | ||
using var httpClient = new HttpClient(); | ||
httpClient.GetAsync($"{address}/").Wait(); | ||
} | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder.UseStartup<Startup>(); | ||
webBuilder.UseUrls($"http://127.0.0.1:0"); | ||
}); | ||
} |
50 changes: 50 additions & 0 deletions
50
test/test-applications/integrations/TestApplication.Razor/Startup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// <copyright file="Startup.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
namespace TestApplication.Razor; | ||
|
||
public class Startup | ||
{ | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddRazorPages(); | ||
} | ||
|
||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger) | ||
{ | ||
if (env.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
} | ||
else | ||
{ | ||
app.UseExceptionHandler("/Home/Error"); | ||
app.UseHsts(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
app.UseStaticFiles(); | ||
|
||
app.UseRouting(); | ||
|
||
app.UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapRazorPages(); | ||
}); | ||
|
||
logger.LogWarning("Startup Configuration is completed."); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
test/test-applications/integrations/TestApplication.Razor/TestApplication.Razor.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net7.0;net6.0</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
</Project> |