This repository was archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Application insights logger lightup #31
Merged
pakrym
merged 7 commits into
rel/2.0.0-preview1
from
pakrym/application-insights-logger
Apr 28, 2017
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
87b881f
Application insights logger lightup
pakrym edbabd4
Rename test folder
pakrym 2a2a9a9
Nits
pakrym d758ca1
Undo change
pakrym 69f507d
Add IISIntegration package references
pakrym d47d31d
PR comments
pakrym bff92a1
Update AI version
pakrym File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
37 changes: 37 additions & 0 deletions
37
sample/ApplicationInsightsHostingStartupSample/CurrentResponseTelemetryChannel.cs
This file contains hidden or 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,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; } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
11 changes: 11 additions & 0 deletions
11
...ationInsightsHostingStartupSample/home/site/diagnostics/ApplicationInsights.settings.json
This file contains hidden or 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,11 @@ | ||
| { | ||
| "Logging": { | ||
| "Microsoft.ApplicationInsights.AspNetCore.Logging.ApplicationInsightsLoggerProvider": { | ||
| "LogLevel": { | ||
| "Microsoft": "Information", | ||
| "Specific": "Trace", | ||
| "Application": "Trace" | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
27 changes: 27 additions & 0 deletions
27
...t.AspNetCore.ApplicationInsights.HostingStartup/ApplicationInsightsLoggerStartupFilter.cs
This file contains hidden or 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,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); | ||
| }; | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -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))] | ||
|
|
@@ -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}"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>(); | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or 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
File renamed without changes.
66 changes: 66 additions & 0 deletions
66
....AspNetCore.ApplicationInsights.HostingStartup.Tests/ApplicationInsightsFunctionalTest.cs
This file contains hidden or 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,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; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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