Application insights logger lightup (#31)
This commit is contained in:
parent
8f65367947
commit
dfd1da68ec
|
|
@ -1,6 +1,6 @@
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
# Visual Studio 15
|
# Visual Studio 15
|
||||||
VisualStudioVersion = 15.0.26228.9
|
VisualStudioVersion = 15.0.26412.1
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.AzureAppServicesIntegration", "src\Microsoft.AspNetCore.AzureAppServicesIntegration\Microsoft.AspNetCore.AzureAppServicesIntegration.csproj", "{5916BEB5-0969-469B-976C-A392E015DFAC}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.AzureAppServicesIntegration", "src\Microsoft.AspNetCore.AzureAppServicesIntegration\Microsoft.AspNetCore.AzureAppServicesIntegration.csproj", "{5916BEB5-0969-469B-976C-A392E015DFAC}"
|
||||||
EndProject
|
EndProject
|
||||||
|
|
@ -33,7 +33,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Web.Xdt.Extension
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.AzureAppServices.SiteExtension", "src\Microsoft.AspNetCore.AzureAppServices.SiteExtension\Microsoft.AspNetCore.AzureAppServices.SiteExtension.csproj", "{1CE2D76B-39E6-46C0-8F6F-C63E370955A9}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.AzureAppServices.SiteExtension", "src\Microsoft.AspNetCore.AzureAppServices.SiteExtension\Microsoft.AspNetCore.AzureAppServices.SiteExtension.csproj", "{1CE2D76B-39E6-46C0-8F6F-C63E370955A9}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApplicationInsightsJavaScriptSnippetTest", "test\ApplicationInsightsJavaScriptSnippetTest\ApplicationInsightsJavaScriptSnippetTest.csproj", "{0899A101-E451-40A4-81B0-7AA18202C25D}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.ApplicationInsights.HostingStartup.Tests", "test\Microsoft.AspNetCore.ApplicationInsights.HostingStartup.Tests\Microsoft.AspNetCore.ApplicationInsights.HostingStartup.Tests.csproj", "{0899A101-E451-40A4-81B0-7AA18202C25D}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<AspNetCoreVersion>2.0.0-preview1-*</AspNetCoreVersion>
|
<AspNetCoreVersion>2.0.0-preview1-*</AspNetCoreVersion>
|
||||||
<AspNetIntegrationTestingVersion>0.4.0-*</AspNetIntegrationTestingVersion>
|
<AspNetIntegrationTestingVersion>0.4.0-*</AspNetIntegrationTestingVersion>
|
||||||
<AppInsightsVersion>2.0.0</AppInsightsVersion>
|
<AppInsightsVersion>2.1.0-beta2</AppInsightsVersion>
|
||||||
<CoreFxVersion>4.3.0</CoreFxVersion>
|
<CoreFxVersion>4.3.0</CoreFxVersion>
|
||||||
<InternalAspNetCoreSdkVersion>2.0.0-*</InternalAspNetCoreSdkVersion>
|
<InternalAspNetCoreSdkVersion>2.0.0-*</InternalAspNetCoreSdkVersion>
|
||||||
<MoqVersion>4.7.1</MoqVersion>
|
<MoqVersion>4.7.1</MoqVersion>
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="$(AspNetCoreVersion)" />
|
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="$(AspNetCoreVersion)" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="$(AspNetCoreVersion)" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="$(AspNetCoreVersion)" />
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="$(AspNetCoreVersion)" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="$(AspNetCoreVersion)" />
|
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="$(AspNetCoreVersion)" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="$(AspNetCoreVersion)" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="$(AspNetCoreVersion)" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(AspNetCoreVersion)" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(AspNetCoreVersion)" />
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
using System;
|
||||||
|
using Microsoft.ApplicationInsights.Channel;
|
||||||
|
using Microsoft.ApplicationInsights.DataContracts;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
|
namespace IISSample
|
||||||
|
{
|
||||||
|
public class CurrentResponseTelemetryChannel : ITelemetryChannel
|
||||||
|
{
|
||||||
|
private readonly HttpResponse _response;
|
||||||
|
|
||||||
|
public CurrentResponseTelemetryChannel(HttpResponse response)
|
||||||
|
{
|
||||||
|
_response = response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Send(ITelemetry item)
|
||||||
|
{
|
||||||
|
if (item is TraceTelemetry traceTelemetry)
|
||||||
|
{
|
||||||
|
_response.WriteAsync(traceTelemetry.Message + Environment.NewLine).GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Flush()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool? DeveloperMode { get; set; }
|
||||||
|
public string EndpointAddress { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using Microsoft.ApplicationInsights.Extensibility;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
@ -20,13 +21,39 @@ namespace IISSample
|
||||||
services.AddMvc();
|
services.AddMvc();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
|
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory, IServiceProvider serviceProvider)
|
||||||
{
|
{
|
||||||
loggerfactory.AddConsole(LogLevel.Debug);
|
loggerFactory.AddConsole(LogLevel.Debug);
|
||||||
|
|
||||||
var logger = loggerfactory.CreateLogger("Requests");
|
var logger = loggerFactory.CreateLogger("Requests");
|
||||||
|
|
||||||
app.UseMvcWithDefaultRoute();
|
app.UseMvcWithDefaultRoute();
|
||||||
|
app.Map("/log", logApp => logApp.Run(async (context) =>
|
||||||
|
{
|
||||||
|
TelemetryConfiguration.Active.TelemetryChannel = new CurrentResponseTelemetryChannel(context.Response);
|
||||||
|
|
||||||
|
var systemLogger = loggerFactory.CreateLogger("System.Namespace");
|
||||||
|
systemLogger.LogTrace("System trace log");
|
||||||
|
systemLogger.LogInformation("System information log");
|
||||||
|
systemLogger.LogWarning("System warning log");
|
||||||
|
|
||||||
|
var microsoftLogger = loggerFactory.CreateLogger("Microsoft.Namespace");
|
||||||
|
microsoftLogger.LogTrace("Microsoft trace log");
|
||||||
|
microsoftLogger.LogInformation("Microsoft information log");
|
||||||
|
microsoftLogger.LogWarning("Microsoft warning log");
|
||||||
|
|
||||||
|
var customLogger = loggerFactory.CreateLogger("Custom.Namespace");
|
||||||
|
customLogger.LogTrace("Custom trace log");
|
||||||
|
customLogger.LogInformation("Custom information log");
|
||||||
|
customLogger.LogWarning("Custom warning log");
|
||||||
|
|
||||||
|
var specificLogger = loggerFactory.CreateLogger("Specific.Namespace");
|
||||||
|
specificLogger.LogTrace("Specific trace log");
|
||||||
|
specificLogger.LogInformation("Specific information log");
|
||||||
|
specificLogger.LogWarning("Specific warning log");
|
||||||
|
|
||||||
|
TelemetryConfiguration.Active.TelemetryChannel = null;
|
||||||
|
}));
|
||||||
app.Run(async (context) =>
|
app.Run(async (context) =>
|
||||||
{
|
{
|
||||||
logger.LogDebug("Received request: " + context.Request.Method + " " + context.Request.Path);
|
logger.LogDebug("Received request: " + context.Request.Method + " " + context.Request.Path);
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,15 @@
|
||||||
},
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"IncludeScopes": false,
|
"IncludeScopes": false,
|
||||||
"LogLevel": {
|
"Debug": {
|
||||||
"Default": "Warning"
|
"LogLevel": {
|
||||||
|
"Default": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Console": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Warning"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"Microsoft.ApplicationInsights.AspNetCore.Logging.ApplicationInsightsLoggerProvider": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Microsoft": "Information",
|
||||||
|
"Specific": "Trace",
|
||||||
|
"Application": "Trace"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="$(AspNetCoreVersion)" />
|
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="$(AspNetCoreVersion)" />
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="$(AspNetCoreVersion)" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="$(AspNetCoreVersion)" />
|
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="$(AspNetCoreVersion)" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(AspNetCoreVersion)" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(AspNetCoreVersion)" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNetCore.ApplicationInsights.HostingStartup
|
||||||
|
{
|
||||||
|
internal class ApplicationInsightsLoggerStartupFilter : IStartupFilter
|
||||||
|
{
|
||||||
|
private readonly Func<string, LogLevel, bool> _noFilter = (s, level) => true;
|
||||||
|
|
||||||
|
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
|
||||||
|
{
|
||||||
|
return builder =>
|
||||||
|
{
|
||||||
|
var loggerFactory = builder.ApplicationServices.GetService<ILoggerFactory>();
|
||||||
|
// We need to disable filtering on logger, filtering would be done by LoggerFactory
|
||||||
|
loggerFactory.AddApplicationInsights(builder.ApplicationServices, _noFilter);
|
||||||
|
next(builder);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,8 +1,14 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Razor.TagHelpers;
|
using Microsoft.AspNetCore.Razor.TagHelpers;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
[assembly: HostingStartup(typeof(Microsoft.AspNetCore.ApplicationInsights.HostingStartup.ApplicationInsightsHostingStartup))]
|
[assembly: HostingStartup(typeof(Microsoft.AspNetCore.ApplicationInsights.HostingStartup.ApplicationInsightsHostingStartup))]
|
||||||
|
|
@ -17,22 +23,60 @@ namespace Microsoft.AspNetCore.ApplicationInsights.HostingStartup
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ApplicationInsightsHostingStartup : IHostingStartup
|
public class ApplicationInsightsHostingStartup : IHostingStartup
|
||||||
{
|
{
|
||||||
|
private const string ApplicationInsightsLoggerFactory = "Microsoft.ApplicationInsights.AspNetCore.Logging.ApplicationInsightsLoggerProvider";
|
||||||
|
private const string ApplicationInsightsSettingsFile = "ApplicationInsights.settings.json";
|
||||||
|
|
||||||
|
private static readonly KeyValuePair<string, string>[] _defaultLoggingLevels = {
|
||||||
|
new KeyValuePair<string, string>("Microsoft", "Warning"),
|
||||||
|
new KeyValuePair<string, string>("System", "Warning"),
|
||||||
|
new KeyValuePair<string, string>("Default", "Information")
|
||||||
|
};
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Calls UseApplicationInsights
|
/// Calls UseApplicationInsights
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="builder"></param>
|
/// <param name="builder"></param>
|
||||||
public void Configure(IWebHostBuilder builder)
|
public void Configure(IWebHostBuilder builder)
|
||||||
{
|
{
|
||||||
|
builder.ConfigureAppConfiguration((context, configurationBuilder) => ConfigureLogging(configurationBuilder));
|
||||||
builder.UseApplicationInsights();
|
builder.UseApplicationInsights();
|
||||||
|
|
||||||
builder.ConfigureServices(InitializeServices);
|
builder.ConfigureServices(InitializeServices);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void ConfigureLogging(IConfigurationBuilder configurationBuilder)
|
||||||
|
{
|
||||||
|
// Skip adding default rules when debugger is attached
|
||||||
|
// we want to send all events to VS
|
||||||
|
if (!Debugger.IsAttached)
|
||||||
|
{
|
||||||
|
configurationBuilder.AddInMemoryCollection(GetDefaultLoggingSettings());
|
||||||
|
}
|
||||||
|
|
||||||
|
var home = Environment.GetEnvironmentVariable("HOME");
|
||||||
|
if (!string.IsNullOrEmpty(home))
|
||||||
|
{
|
||||||
|
var settingsFile = Path.Combine(home, "site", "diagnostics", ApplicationInsightsSettingsFile);
|
||||||
|
configurationBuilder.AddJsonFile(settingsFile, optional: true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static KeyValuePair<string, string>[] GetDefaultLoggingSettings()
|
||||||
|
{
|
||||||
|
return _defaultLoggingLevels.Select(pair =>
|
||||||
|
{
|
||||||
|
var key = $"Logging:{ApplicationInsightsLoggerFactory}:LogLevel:{pair.Key}";
|
||||||
|
return new KeyValuePair<string, string>(key, pair.Value);
|
||||||
|
}).ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds the Javascript <see cref="TagHelperComponent"/> to the <see cref="IServiceCollection"/>.
|
/// Adds the Javascript <see cref="TagHelperComponent"/> to the <see cref="IServiceCollection"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="services">The <see cref="IServiceCollection"/> associated with the application.</param>
|
/// <param name="services">The <see cref="IServiceCollection"/> associated with the application.</param>
|
||||||
private void InitializeServices(IServiceCollection services)
|
private void InitializeServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
services.AddSingleton<IStartupFilter, ApplicationInsightsLoggerStartupFilter>();
|
||||||
services.AddSingleton<ITagHelperComponent, JavaScriptSnippetTagHelperComponent>();
|
services.AddSingleton<ITagHelperComponent, JavaScriptSnippetTagHelperComponent>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@
|
||||||
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="$(AppInsightsVersion)" />
|
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="$(AppInsightsVersion)" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="$(AspNetCoreVersion)" />
|
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="$(AspNetCoreVersion)" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Razor.Runtime" Version="$(AspNetCoreVersion)" />
|
<PackageReference Include="Microsoft.AspNetCore.Razor.Runtime" Version="$(AspNetCoreVersion)" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(AspNetCoreVersion)" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
@ -0,0 +1,66 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using Microsoft.Extensions.Logging.Testing;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
|
namespace ApplicationInsightsJavaScriptSnippetTest
|
||||||
|
{
|
||||||
|
public class ApplicationInsightsFunctionalTest : LoggedTest
|
||||||
|
{
|
||||||
|
public ApplicationInsightsFunctionalTest(ITestOutputHelper output) : base(output)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static string GetApplicationPath()
|
||||||
|
{
|
||||||
|
var current = new DirectoryInfo(AppContext.BaseDirectory);
|
||||||
|
while (current != null)
|
||||||
|
{
|
||||||
|
if (File.Exists(Path.Combine(current.FullName, "AzureIntegration.sln")))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
current = current.Parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current == null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Could not find the solution directory");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Path.GetFullPath(Path.Combine(current.FullName, "sample", "ApplicationInsightsHostingStartupSample"));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static bool PreservePublishedApplicationForDebugging
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var deletePublishedFolder = Environment.GetEnvironmentVariable("ASPNETCORE_DELETEPUBLISHEDFOLDER");
|
||||||
|
|
||||||
|
if (string.Equals("false", deletePublishedFolder, StringComparison.OrdinalIgnoreCase)
|
||||||
|
|| string.Equals("0", deletePublishedFolder, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
// preserve the published folder and do not delete it
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// do not preserve the published folder and delete it
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static string GetCurrentBuildConfiguration()
|
||||||
|
{
|
||||||
|
var configuration = "Debug";
|
||||||
|
if (string.Equals(Environment.GetEnvironmentVariable("Configuration"), "Release", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
configuration = "Release";
|
||||||
|
}
|
||||||
|
|
||||||
|
return configuration;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -14,7 +14,7 @@ using Xunit.Abstractions;
|
||||||
|
|
||||||
namespace ApplicationInsightsJavaScriptSnippetTest
|
namespace ApplicationInsightsJavaScriptSnippetTest
|
||||||
{
|
{
|
||||||
public class ApplicationInsightsJavaScriptSnippetTest : LoggedTest
|
public class ApplicationInsightsJavaScriptSnippetTest : ApplicationInsightsFunctionalTest
|
||||||
{
|
{
|
||||||
public ApplicationInsightsJavaScriptSnippetTest(ITestOutputHelper output) : base(output)
|
public ApplicationInsightsJavaScriptSnippetTest(ITestOutputHelper output) : base(output)
|
||||||
{
|
{
|
||||||
|
|
@ -34,7 +34,7 @@ namespace ApplicationInsightsJavaScriptSnippetTest
|
||||||
using (StartLog(out var loggerFactory, testName))
|
using (StartLog(out var loggerFactory, testName))
|
||||||
{
|
{
|
||||||
var logger = loggerFactory.CreateLogger(nameof(ApplicationInsightsJavaScriptSnippetTest));
|
var logger = loggerFactory.CreateLogger(nameof(ApplicationInsightsJavaScriptSnippetTest));
|
||||||
var deploymentParameters = new DeploymentParameters(GetApplicationPath(applicationType), ServerType.Kestrel, RuntimeFlavor.CoreClr, RuntimeArchitecture.x64)
|
var deploymentParameters = new DeploymentParameters(GetApplicationPath(), ServerType.Kestrel, RuntimeFlavor.CoreClr, RuntimeArchitecture.x64)
|
||||||
{
|
{
|
||||||
PublishApplicationBeforeDeployment = true,
|
PublishApplicationBeforeDeployment = true,
|
||||||
PreservePublishedApplicationForDebugging = PreservePublishedApplicationForDebugging,
|
PreservePublishedApplicationForDebugging = PreservePublishedApplicationForDebugging,
|
||||||
|
|
@ -79,54 +79,5 @@ namespace ApplicationInsightsJavaScriptSnippetTest
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetApplicationPath(ApplicationType applicationType)
|
|
||||||
{
|
|
||||||
var current = new DirectoryInfo(AppContext.BaseDirectory);
|
|
||||||
while (current != null)
|
|
||||||
{
|
|
||||||
if (File.Exists(Path.Combine(current.FullName, "AzureIntegration.sln")))
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
current = current.Parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current == null)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException("Could not find the solution directory");
|
|
||||||
}
|
|
||||||
|
|
||||||
return Path.GetFullPath(Path.Combine(current.FullName, "sample", "ApplicationInsightsHostingStartupSample"));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool PreservePublishedApplicationForDebugging
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
var deletePublishedFolder = Environment.GetEnvironmentVariable("ASPNETCORE_DELETEPUBLISHEDFOLDER");
|
|
||||||
|
|
||||||
if (string.Equals("false", deletePublishedFolder, StringComparison.OrdinalIgnoreCase)
|
|
||||||
|| string.Equals("0", deletePublishedFolder, StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
// preserve the published folder and do not delete it
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// do not preserve the published folder and delete it
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string GetCurrentBuildConfiguration()
|
|
||||||
{
|
|
||||||
var configuration = "Debug";
|
|
||||||
if (string.Equals(Environment.GetEnvironmentVariable("Configuration"), "Release", StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
configuration = "Release";
|
|
||||||
}
|
|
||||||
|
|
||||||
return configuration;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,95 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Server.IntegrationTesting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
|
namespace ApplicationInsightsJavaScriptSnippetTest
|
||||||
|
{
|
||||||
|
public class ApplicationInsightsLoggingTest : ApplicationInsightsFunctionalTest
|
||||||
|
{
|
||||||
|
public ApplicationInsightsLoggingTest(ITestOutputHelper output) : base(output)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(ApplicationType.Portable)]
|
||||||
|
[InlineData(ApplicationType.Standalone)]
|
||||||
|
public async Task ScriptInjected(ApplicationType applicationType)
|
||||||
|
{
|
||||||
|
var testName = $"ApplicationInsightsLoggingTest_{applicationType}";
|
||||||
|
using (StartLog(out var loggerFactory, testName))
|
||||||
|
{
|
||||||
|
var logger = loggerFactory.CreateLogger(nameof(ApplicationInsightsJavaScriptSnippetTest));
|
||||||
|
var deploymentParameters = new DeploymentParameters(GetApplicationPath(), ServerType.Kestrel, RuntimeFlavor.CoreClr, RuntimeArchitecture.x64)
|
||||||
|
{
|
||||||
|
PublishApplicationBeforeDeployment = true,
|
||||||
|
PreservePublishedApplicationForDebugging = PreservePublishedApplicationForDebugging,
|
||||||
|
TargetFramework = "netcoreapp2.0",
|
||||||
|
Configuration = GetCurrentBuildConfiguration(),
|
||||||
|
ApplicationType = applicationType,
|
||||||
|
EnvironmentVariables =
|
||||||
|
{
|
||||||
|
new KeyValuePair<string, string>(
|
||||||
|
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES",
|
||||||
|
"Microsoft.AspNetCore.ApplicationInsights.HostingStartup"),
|
||||||
|
new KeyValuePair<string, string>(
|
||||||
|
"HOME",
|
||||||
|
Path.Combine(GetApplicationPath(), "home")),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
using (var deployer = ApplicationDeployerFactory.Create(deploymentParameters, loggerFactory))
|
||||||
|
{
|
||||||
|
var deploymentResult = await deployer.DeployAsync();
|
||||||
|
var httpClientHandler = new HttpClientHandler();
|
||||||
|
var httpClient = deploymentResult.CreateHttpClient(httpClientHandler);
|
||||||
|
|
||||||
|
// Request to base address and check if various parts of the body are rendered & measure the cold startup time.
|
||||||
|
var response = await RetryHelper.RetryRequest(
|
||||||
|
async () => await httpClient.GetAsync("/log"),
|
||||||
|
logger: logger, cancellationToken: deploymentResult.HostShutdownToken);
|
||||||
|
|
||||||
|
Assert.False(response == null, "Response object is null because the client could not " +
|
||||||
|
"connect to the server after multiple retries");
|
||||||
|
|
||||||
|
var responseText = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
// Enabled by default
|
||||||
|
Assert.Contains("System warning log", responseText);
|
||||||
|
// Disabled by default
|
||||||
|
Assert.DoesNotContain("System information log", responseText);
|
||||||
|
// Disabled by default
|
||||||
|
Assert.DoesNotContain("System trace log", responseText);
|
||||||
|
|
||||||
|
// Enabled by default
|
||||||
|
Assert.Contains("Microsoft warning log", responseText);
|
||||||
|
// Disabled by default but overridden by ApplicationInsights.settings.json
|
||||||
|
Assert.Contains("Microsoft information log", responseText);
|
||||||
|
// Disabled by default
|
||||||
|
Assert.DoesNotContain("Microsoft trace log", responseText);
|
||||||
|
|
||||||
|
// Enabled by default
|
||||||
|
Assert.Contains("Custom warning log", responseText);
|
||||||
|
// Enabled by default
|
||||||
|
Assert.Contains("Custom information log", responseText);
|
||||||
|
// Disabled by default
|
||||||
|
Assert.DoesNotContain("Custom trace log", responseText);
|
||||||
|
|
||||||
|
// Enabled by default
|
||||||
|
Assert.Contains("Specific warning log", responseText);
|
||||||
|
// Enabled by default
|
||||||
|
Assert.Contains("Specific information log", responseText);
|
||||||
|
// Disabled by default but overridden by ApplicationInsights.settings.json
|
||||||
|
Assert.Contains("Specific trace log", responseText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue