From 55d06f88ee2d564e0ef01d6fca3bfbd3c8ff67a9 Mon Sep 17 00:00:00 2001 From: Cathal McHale Date: Mon, 28 May 2018 11:37:25 +0100 Subject: [PATCH 1/5] Post-build step so can simply "Import-Module" on the psm1 file and have the dll co-resident (all in bin output dir). --- PSLogging.csproj | 4 +++- targets/PowerShellLoggingModule.targets | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 targets/PowerShellLoggingModule.targets diff --git a/PSLogging.csproj b/PSLogging.csproj index 6dd6501..7350a21 100644 --- a/PSLogging.csproj +++ b/PSLogging.csproj @@ -61,11 +61,13 @@ + + - --> \ No newline at end of file diff --git a/targets/PowerShellLoggingModule.targets b/targets/PowerShellLoggingModule.targets new file mode 100644 index 0000000..83e7a12 --- /dev/null +++ b/targets/PowerShellLoggingModule.targets @@ -0,0 +1,23 @@ + + + + $(SolutionDir)Module\PowerShellLogging\ + $(SolutionDir)bin\$(Configuration)\ + + + + + + + + + + + + + + + + \ No newline at end of file From 2762b6ff37a597cfc785d1e05a24527328c1b823 Mon Sep 17 00:00:00 2001 From: Cathal McHale Date: Mon, 28 May 2018 16:36:53 +0100 Subject: [PATCH 2/5] Added the ApplicationInsights subscriber - all that's missing is the line to actually log to Azure. Will add azure interaction in next commit, which will require a nuget ref - this is a dependency that the original project did not require. --- ApplicationInsightsOutputSubscriber.cs | 100 ++++++++++++++++++ Commands/DisableApplicationInsightsCommand.cs | 26 +++++ Commands/EnableApplicationInsightsCommand.cs | 68 ++++++++++++ PSLogging.csproj | 3 + 4 files changed, 197 insertions(+) create mode 100644 ApplicationInsightsOutputSubscriber.cs create mode 100644 Commands/DisableApplicationInsightsCommand.cs create mode 100644 Commands/EnableApplicationInsightsCommand.cs diff --git a/ApplicationInsightsOutputSubscriber.cs b/ApplicationInsightsOutputSubscriber.cs new file mode 100644 index 0000000..571e42d --- /dev/null +++ b/ApplicationInsightsOutputSubscriber.cs @@ -0,0 +1,100 @@ +// ReSharper disable MemberCanBePrivate.Global +// ReSharper disable UnusedMember.Global + +namespace PSLogging +{ + using System; + using System.IO; + using System.Management.Automation; + + public class ApplicationInsightsOutputSubscriber : HostIOSubscriberBase + { + private readonly string key; + + public ApplicationInsightsOutputSubscriber(string key, // Azure Application Insights "InstrumentationKey" + string dateTimeFormat = "r") + { + this.key = key; + DateTimeFormat = dateTimeFormat; + } + + + #region Properties + + public string DateTimeFormat { get; set; } + + #endregion + + + public override void WriteDebug(string message) + { + if (string.IsNullOrEmpty(key) || + string.IsNullOrEmpty(message)) + { + return; + } + + if (message.Trim() != String.Empty) + { + message = String.Format("{0,-29} - [D] {1}", DateTime.UtcNow.ToString(DateTimeFormat), message); + } + + // TODO: Call onto nuget referenced ApplicationInsights dll + + } + + public override void WriteError(string message) + { + if (string.IsNullOrEmpty(key) || + string.IsNullOrEmpty(message)) + { + return; + } + + if (message.Trim() != String.Empty) + { + message = String.Format("{0,-29} - [E] {1}", DateTime.UtcNow.ToString(DateTimeFormat), message); + } + + // TODO: Call onto nuget referenced ApplicationInsights dll + + } + + public override void WriteVerbose(string message) + { + if (string.IsNullOrEmpty(key) || + string.IsNullOrEmpty(message)) + { + return; + } + + if (message.Trim() != String.Empty) + { + message = String.Format("{0,-29} - [V] {1}", DateTime.UtcNow.ToString(DateTimeFormat), message); + } + + // TODO: Call onto nuget referenced ApplicationInsights dll + + } + + public override void WriteWarning(string message) + { + if (string.IsNullOrEmpty(key) || + string.IsNullOrEmpty(message)) + { + return; + } + + if (message.Trim() != String.Empty) + { + message = String.Format("{0,-29} - [W] {1}", DateTime.UtcNow.ToString(DateTimeFormat), message); + } + + // TODO: Call onto nuget referenced ApplicationInsights dll + + } + } +} + +// ReSharper restore MemberCanBePrivate.Global +// ReSharper restore UnusedMember.Global \ No newline at end of file diff --git a/Commands/DisableApplicationInsightsCommand.cs b/Commands/DisableApplicationInsightsCommand.cs new file mode 100644 index 0000000..7f9bace --- /dev/null +++ b/Commands/DisableApplicationInsightsCommand.cs @@ -0,0 +1,26 @@ +// ReSharper disable MemberCanBePrivate.Global +// ReSharper disable UnusedAutoPropertyAccessor.Global +// ReSharper disable UnusedMember.Global + +namespace PSLogging.Commands +{ + using System.Management.Automation; + + [Cmdlet(VerbsLifecycle.Disable, "ApplicationInsights")] + public class DisableApplicationInsightsCommand : PSCmdlet + { + [Parameter(Mandatory = true, + ValueFromPipeline = true, + Position = 0)] + public ApplicationInsightsOutputSubscriber InputObject { get; set; } + + protected override void EndProcessing() + { + HostIOInterceptor.Instance.RemoveSubscriber(InputObject); + } + } +} + +// ReSharper restore MemberCanBePrivate.Global +// ReSharper restore UnusedAutoPropertyAccessor.Global +// ReSharper restore UnusedMember.Global \ No newline at end of file diff --git a/Commands/EnableApplicationInsightsCommand.cs b/Commands/EnableApplicationInsightsCommand.cs new file mode 100644 index 0000000..6c7c2f5 --- /dev/null +++ b/Commands/EnableApplicationInsightsCommand.cs @@ -0,0 +1,68 @@ +// ReSharper disable MemberCanBePrivate.Global +// ReSharper disable UnusedAutoPropertyAccessor.Global +// ReSharper disable UnusedMember.Global + +namespace PSLogging.Commands +{ + using System.Management.Automation; + + [Cmdlet(VerbsLifecycle.Enable, "ApplicationInsights")] + public class EnableApplicationInsightsCommand : PSCmdlet + { + private ApplicationInsightsOutputSubscriber inputObject; + private string key; + private string dateTimeFormat = "r"; + + #region Parameters + + [Parameter(ParameterSetName = "AttachExisting", + Mandatory = true, + Position = 0, + ValueFromPipeline = true)] + public ApplicationInsightsOutputSubscriber InputObject + { + get { return inputObject; } + set { inputObject = value; } + } + + [Parameter(Mandatory = true, + Position = 0, + ParameterSetName = "New")] + public string Key + { + get { return key; } + set { key = value; } + } + + [Parameter(ParameterSetName = "New")] + public string DateTimeFormat + { + get { return dateTimeFormat; } + set { dateTimeFormat = value; } + } + + #endregion + + protected override void EndProcessing() + { + ApplicationInsightsOutputSubscriber subscriber; + + if (ParameterSetName == "New") + { + subscriber = new ApplicationInsightsOutputSubscriber(key, dateTimeFormat); + WriteObject(subscriber); + } + else + { + subscriber = inputObject; + } + + HostIOInterceptor.Instance.AttachToHost(Host); + HostIOInterceptor.Instance.AddSubscriber(subscriber); + } + } +} + +// ReSharper restore MemberCanBePrivate.Global +// ReSharper restore UnusedAutoPropertyAccessor.Global +// ReSharper restore UnusedMember.Global \ No newline at end of file diff --git a/PSLogging.csproj b/PSLogging.csproj index 7350a21..ae00f2d 100644 --- a/PSLogging.csproj +++ b/PSLogging.csproj @@ -39,6 +39,9 @@ + + + From 398352bd0a2fb2bc2519cbda1e8588a72e93d9b9 Mon Sep 17 00:00:00 2001 From: Cathal McHale Date: Mon, 28 May 2018 16:50:19 +0100 Subject: [PATCH 3/5] Upgrade target .NET framework so that can add nuget dependency on ApplicationInsights. --- PSLogging.csproj | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/PSLogging.csproj b/PSLogging.csproj index ae00f2d..e90a2c9 100644 --- a/PSLogging.csproj +++ b/PSLogging.csproj @@ -1,5 +1,5 @@  - + Debug @@ -9,7 +9,7 @@ Properties PSLogging PowerShellLoggingModule - v2.0 + v4.6.1 512 @@ -22,6 +22,7 @@ DEBUG;TRACE prompt 4 + false pdbonly @@ -30,6 +31,7 @@ TRACE prompt 4 + false From 82164d64ef6735d19b16e65b7cfd3df213232ec7 Mon Sep 17 00:00:00 2001 From: Cathal McHale Date: Mon, 28 May 2018 16:54:38 +0100 Subject: [PATCH 4/5] Add nuget ref to ApplicationInsights --- .gitignore | 9 +++++++++ PSLogging.csproj | 10 ++++++++++ packages.config | 5 +++++ 3 files changed, 24 insertions(+) create mode 100644 packages.config diff --git a/.gitignore b/.gitignore index 2329206..c499cb0 100644 --- a/.gitignore +++ b/.gitignore @@ -95,6 +95,15 @@ ipch/ # Guidance Automation Toolkit *.gpState +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config + # ReSharper is a .NET coding add-in _ReSharper*/ *.[Rr]e[Ss]harper diff --git a/PSLogging.csproj b/PSLogging.csproj index e90a2c9..cc99b10 100644 --- a/PSLogging.csproj +++ b/PSLogging.csproj @@ -34,11 +34,18 @@ false + + packages\Microsoft.ApplicationInsights.2.6.4\lib\net46\Microsoft.ApplicationInsights.dll + + + packages\System.Diagnostics.DiagnosticSource.4.4.0\lib\net46\System.Diagnostics.DiagnosticSource.dll + False ..\..\..\..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll + @@ -65,6 +72,9 @@ Designer + + +