Skip to content

Commit

Permalink
Merge pull request #99 from christianhelle/development
Browse files Browse the repository at this point in the history
Enable Error Logging to Azure Application Insights
  • Loading branch information
christianhelle authored Aug 31, 2022
2 parents 529ab53 + dfb0f88 commit ade1a24
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/ReswCodeGen.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28407.52
# Visual Studio Version 17
VisualStudioVersion = 17.4.32804.182
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VSPackage", "VSPackage\VSPackage.csproj", "{05F77BFB-CE11-4948-8174-F938C5B7F31C}"
EndProject
Expand Down
3 changes: 1 addition & 2 deletions src/VSPackage.Tests/CSharpCodeGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.IO;
using System.Linq;
using ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand Down Expand Up @@ -77,7 +76,7 @@ public void ClassNameEqualsFileNameWithoutExtension()
[TestMethod]
public void ResourceLoaderInitializedWithClassName()
{
Assert.IsTrue(actual.Contains("new ResourceLoader(currentAssemblyName + \"/Resources\");"));
Assert.IsTrue(actual.Contains("ResourceLoader.GetForCurrentView(currentAssemblyName + \"/Resources\");"));
}

[TestMethod]
Expand Down
2 changes: 1 addition & 1 deletion src/VSPackage.Tests/CSharpCodeGeneratorTestsInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void ClassNameEqualsFileNameWithoutExtension()
[TestMethod]
public void ResourceLoaderInitializedWithClassName()
{
Assert.IsTrue(actual.Contains("new ResourceLoader(currentAssemblyName + \"/Resources\");"));
Assert.IsTrue(actual.Contains("ResourceLoader.GetForCurrentView(currentAssemblyName + \"/Resources\");"));
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void ClassNameEqualsFileNameWithoutExtension()
[TestMethod]
public void ResourceLoaderInitializedWithClassName()
{
Assert.IsTrue(actual.Contains("New ResourceLoader(currentAssemblyName + \"/Resources\")"));
Assert.IsTrue(actual.Contains("ResourceLoader.GetForCurrentView(currentAssemblyName + \"/Resources\")"));
}

[TestMethod]
Expand Down
2 changes: 1 addition & 1 deletion src/VSPackage.Tests/VisualBasicCodeGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void ClassNameEqualsFileNameWithoutExtension()
[TestMethod]
public void ResourceLoaderInitializedWithClassName()
{
Assert.IsTrue(actual.Contains("New ResourceLoader(currentAssemblyName + \"/Resources\")"));
Assert.IsTrue(actual.Contains("ResourceLoader.GetForCurrentView(currentAssemblyName + \"/Resources\")"));
}

[TestMethod]
Expand Down
61 changes: 61 additions & 0 deletions src/VSPackage/CustomTool/AppInsightsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;

namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool
{
[ExcludeFromCodeCoverage]
public class AppInsightsClient
{
private readonly TelemetryClient telemetryClient = null;

public AppInsightsClient()
{
try
{
var configuration = TelemetryConfiguration.CreateDefault();
configuration.InstrumentationKey = "eba88f76-06da-4ef8-a0fe-162b1a70f871";
//// configuration.ConnectionString = "InstrumentationKey=eba88f76-06da-4ef8-a0fe-162b1a70f871;IngestionEndpoint=https://westeurope-5.in.applicationinsights.azure.com/;LiveEndpoint=https://westeurope.livediagnostics.monitor.azure.com/";

telemetryClient = new TelemetryClient(configuration);
}
catch (Exception ex1)
{
Trace.WriteLine(ex1);

try
{
telemetryClient = new TelemetryClient();
#pragma warning disable CS0618 // Type or member is obsolete
telemetryClient.InstrumentationKey = "eba88f76-06da-4ef8-a0fe-162b1a70f871";
#pragma warning restore CS0618 // Type or member is obsolete
}
catch (Exception ex2)
{
Trace.WriteLine(ex2);
return;
}
}

telemetryClient.Context.Session.Id = Guid.NewGuid().ToString();
telemetryClient.Context.Device.OperatingSystem = Environment.OSVersion.ToString();
telemetryClient.Context.Component.Version = GetType().Assembly.GetName().Version.ToString();
}

public static AppInsightsClient Instance { get; } = new AppInsightsClient();

public void TrackFeatureUsage(string featureName, params string[] tags)
{
telemetryClient?.TrackEvent(featureName);
telemetryClient?.Flush();
}

public void TrackError(Exception exception)
{
telemetryClient?.TrackException(exception);
telemetryClient?.Flush();
}
}
}
10 changes: 6 additions & 4 deletions src/VSPackage/CustomTool/ReswFileCSharpCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool
[Guid("98983F6D-BC77-46AC-BA5A-8D9E8763F0D2")]
[ComVisible(true)]
[ProvideObject(typeof(ReswFileCSharpCodeGenerator))]
[CodeGeneratorRegistration(typeof(ReswFileCSharpCodeGenerator),
"C# ResW File Code Generator",
Guids.ReswFileCSharpCodeGenerator,
GeneratesDesignTimeSource = true,
[CodeGeneratorRegistration(typeof(ReswFileCSharpCodeGenerator),
"C# ResW File Code Generator",
Guids.ReswFileCSharpCodeGenerator,
GeneratesDesignTimeSource = true,
GeneratorRegKeyName = "ReswFileCodeGenerator")]
public class ReswFileCSharpCodeGenerator : ReswFileCodeGenerator
{
Expand All @@ -19,6 +19,8 @@ public ReswFileCSharpCodeGenerator()
{
}

protected override string GeneratorName => "C# ResW File Code Generator";

public override int DefaultExtension(out string pbstrDefaultExtension)
{
pbstrDefaultExtension = ".cs";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public ReswFileCSharpCodeGeneratorInternal()
{
}

protected override string GeneratorName => "C# ResW File Code Generator (Internal class)";

public override int DefaultExtension(out string pbstrDefaultExtension)
{
pbstrDefaultExtension = ".cs";
Expand Down
9 changes: 7 additions & 2 deletions src/VSPackage/CustomTool/ReswFileCodeGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualStudio.Shell.Interop;

Expand All @@ -15,6 +13,8 @@ public abstract class ReswFileCodeGenerator : IVsSingleFileGenerator
private readonly CodeDomProvider codeDomProvider;
private readonly TypeAttributes? classAccessibility;

protected abstract string GeneratorName { get; }

protected ReswFileCodeGenerator(CodeDomProvider codeDomProvider, TypeAttributes? classAccessibility = null)
{
this.codeDomProvider = codeDomProvider;
Expand Down Expand Up @@ -43,9 +43,14 @@ public virtual int Generate(string wszInputFilePath,
}
catch (Exception e)
{
AppInsightsClient.Instance.TrackError(e);
MessageBox.Show(e.Message, "Unable to generate code");
throw;
}
finally
{
AppInsightsClient.Instance.TrackFeatureUsage(GeneratorName);
}

return 0;
}
Expand Down
4 changes: 3 additions & 1 deletion src/VSPackage/CustomTool/ReswFileVisualBasicCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool
[ProvideObject(typeof(ReswFileVisualBasicCodeGenerator))]
[CodeGeneratorRegistration(typeof(ReswFileVisualBasicCodeGenerator),
"Visual Basic ResW File Code Generator",
Guids.ReswFileVisualBasicCodeGenerator,
Guids.ReswFileVisualBasicCodeGenerator,
GeneratesDesignTimeSource = true,
GeneratorRegKeyName = "ReswFileCodeGenerator")]
public class ReswFileVisualBasicCodeGenerator : ReswFileCodeGenerator
Expand All @@ -19,6 +19,8 @@ public ReswFileVisualBasicCodeGenerator()
{
}

protected override string GeneratorName => "Visual Basic ResW File Code Generator";

public override int DefaultExtension(out string pbstrDefaultExtension)
{
pbstrDefaultExtension = ".vb";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public ReswFileVisualBasicCodeGeneratorInternal()
{
}

protected override string GeneratorName => "Visual Basic ResW File Code Generator (Internal class)";

public override int DefaultExtension(out string pbstrDefaultExtension)
{
pbstrDefaultExtension = ".vb";
Expand Down
6 changes: 5 additions & 1 deletion src/VSPackage/VSPackage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="CustomTool\AppInsightsClient.cs" />
<None Include="source.extension.vsixmanifest">
<SubType>Designer</SubType>
</None>
Expand All @@ -93,7 +94,10 @@
<IncludeInVSIX>true</IncludeInVSIX>
</Content>
</ItemGroup>
<ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.WindowsServer">
<Version>2.10.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.SDK">
<Version>15.0.1</Version>
</PackageReference>
Expand Down

0 comments on commit ade1a24

Please sign in to comment.