Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions AzureIntegration.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26228.9
VisualStudioVersion = 15.0.26412.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}"
EndProject
Expand Down Expand Up @@ -33,7 +33,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Web.Xdt.Extension
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}"
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
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
2 changes: 1 addition & 1 deletion build/dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<AspNetCoreVersion>2.0.0-preview1-*</AspNetCoreVersion>
<AspNetIntegrationTestingVersion>0.4.0-*</AspNetIntegrationTestingVersion>
<AppInsightsVersion>2.0.0</AppInsightsVersion>
<AppInsightsVersion>2.1.0-beta2</AppInsightsVersion>
<CoreFxVersion>4.3.0</CoreFxVersion>
<InternalAspNetCoreSdkVersion>2.0.0-*</InternalAspNetCoreSdkVersion>
<MoqVersion>4.7.1</MoqVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Hosting" 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.Extensions.Configuration.CommandLine" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(AspNetCoreVersion)" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
}
33 changes: 30 additions & 3 deletions sample/ApplicationInsightsHostingStartupSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.IO;
using System.Linq;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
Expand All @@ -20,13 +21,39 @@ public void ConfigureServices(IServiceCollection services)
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to be a warning when I push aspnet/Logging#605


var logger = loggerfactory.CreateLogger("Requests");
var logger = loggerFactory.CreateLogger("Requests");

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) =>
{
logger.LogDebug("Received request: " + context.Request.Method + " " + context.Request.Path);
Expand Down
11 changes: 9 additions & 2 deletions sample/ApplicationInsightsHostingStartupSample/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Logging": {
"Microsoft.ApplicationInsights.AspNetCore.Logging.ApplicationInsightsLoggerProvider": {
"LogLevel": {
"Microsoft": "Information",
"Specific": "Trace",
"Application": "Trace"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<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.Extensions.Logging.Console" Version="$(AspNetCoreVersion)" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
// 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.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

[assembly: HostingStartup(typeof(Microsoft.AspNetCore.ApplicationInsights.HostingStartup.ApplicationInsightsHostingStartup))]
Expand All @@ -17,22 +23,60 @@ namespace Microsoft.AspNetCore.ApplicationInsights.HostingStartup
/// </summary>
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>
/// Calls UseApplicationInsights
/// </summary>
/// <param name="builder"></param>
public void Configure(IWebHostBuilder builder)
{
builder.ConfigureAppConfiguration((context, configurationBuilder) => ConfigureLogging(configurationBuilder));
builder.UseApplicationInsights();

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}";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you extract just this part of the string as a constant when it's not used elsewhere?

Copy link
Contributor Author

@pakrym pakrym Apr 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly because it was just too long

return new KeyValuePair<string, string>(key, pair.Value);
}).ToArray();
}

/// <summary>
/// Adds the Javascript <see cref="TagHelperComponent"/> to the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> associated with the application.</param>
private void InitializeServices(IServiceCollection services)
{
services.AddSingleton<IStartupFilter, ApplicationInsightsLoggerStartupFilter>();
services.AddSingleton<ITagHelperComponent, JavaScriptSnippetTagHelperComponent>();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="$(AppInsightsVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Runtime" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(AspNetCoreVersion)" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Loading