Skip to content

Commit 6f1b313

Browse files
authored
Refactor 2 finish. (#9)
Loglevel added, classification changed to optional, swagger code fixes to eventspost/async with reponse to populate data with server message. Added EnvLanguage and deployment stage property, name now becomes the runtime interpreter name and envVersion now works as intended.
1 parent 71825b5 commit 6f1b313

File tree

5 files changed

+142
-57
lines changed

5 files changed

+142
-57
lines changed

IO.TrakerrClient/TrakerrClient.cs

Lines changed: 128 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using IO.Trakerr.Api;
22
using IO.Trakerr.Client;
33
using IO.Trakerr.Model;
4+
using Microsoft.Win32;
45
using System;
56
using System.Configuration;
67
using System.Net;
78
using System.Net.Sockets;
9+
using System.Reflection;
810

911
/// <summary>
1012
/// Trakerr.IO namespace
@@ -22,16 +24,12 @@ public static class TrakerrException
2224
/// Send an exception to Trakerr.
2325
/// </summary>
2426
/// <param name="e">The exception caught.</param>
25-
/// <param name="classification">The classification ("Error", "Warning", "Info", "Debug")</param>
26-
public static void SendToTrakerr(this Exception e, string classification = "Error")
27+
/// <param name="classification">Optional extra string descriptor. Defaults to issue.</param>
28+
public static void SendToTrakerr(this Exception e, string classification = "issue")
2729
{
2830
var client = new TrakerrClient();
2931

30-
var exceptionEvent = client.CreateAppEvent(classification, e.GetType().ToString(), e.Message);
31-
32-
exceptionEvent.EventStacktrace = EventTraceBuilder.GetEventTraces(e);
33-
34-
client.SendEventAsync(exceptionEvent);
32+
client.SendEventAsync(client.CreateAppEvent(e, classification));
3533
}
3634
}
3735

@@ -60,112 +58,147 @@ public class TrakerrClient
6058

6159
public string apiKey { get; set; }
6260
public string contextAppVersion { get; set; }
61+
public string contextDeploymentStage { get; set; }
62+
63+
/// <summary>
64+
/// A constant property with the name of the language this is compiled from. Is set in a constructor argument (so you can pass in managed c++ or VB), but defaults to c#
65+
/// </summary>
66+
public string contextEnvLanguage { get; }
67+
68+
/// <summary>
69+
/// Name of the CLI the program is running on.
70+
/// </summary>
6371
public string contextEnvName { get; set; }
6472

6573
/// <summary>
6674
/// The version of the CLI the application is run on.
6775
/// </summary>
68-
public string ContextEnvVersion { get; set; }
76+
public string contextEnvVersion { get; set; }
6977

7078
/// <summary>
7179
/// The hostname of the pc running the application.
7280
/// </summary>
73-
public string ContextEnvHostname { get; set; }
81+
public string contextEnvHostname { get; set; }
7482

7583
/// <summary>
7684
/// The OS the application is running on.
7785
/// </summary>
78-
public string ContextAppOS { get; set; }
86+
public string contextAppOS { get; set; }
7987

8088
/// <summary>
8189
/// The version of the OS the application is running on.
8290
/// </summary>
83-
public string ContextAppOSVersion { get; set; }
91+
public string contextAppOSVersion { get; set; }
8492

8593
/// <summary>
8694
/// Optional. Useful For MVC and ASP.net applications the browser name the application is running on.
8795
/// </summary>
88-
public string ContextAppOSBrowser { get; set; }
96+
public string contextAppOSBrowser { get; set; }
8997

9098
/// <summary>
9199
/// Optional. Useful for MVC and ASP.net applications the browser version the application is running on.
92100
/// </summary>
93-
public string ContextAppOSBrowserVersion { get; set; }
101+
public string contextAppOSBrowserVersion { get; set; }
94102

95103
/// <summary>
96104
/// Optional. Datacenter the application may be running on.
97105
/// </summary>
98-
public string ContextDataCenter { get; set; }
106+
public string contextDataCenter { get; set; }
99107

100108
/// <summary>
101109
/// Optional. Datacenter region the application may be running on.
102110
/// </summary>
103-
public string ContextDataCenterRegion { get; set; }
111+
public string contextDataCenterRegion { get; set; }
104112

105113
/// <summary>
106114
/// Create a new Trakerr client to use in your application. This class is thread-safe and can be invoked from multiple threads. This class also acts as a factory to create new AppEvent's with the supplied apiKey and other data.
107115
/// </summary>
108116
/// <param name="apiKey">API Key for your application, defaults to reading "trakerr.apiKey" property under appSettings from the App.config.</param>
109117
/// <param name="contextAppVersion">Provide the application version, defaults to reading "trakerr.contextAppVersion" property under appSettings from the App.config.</param>
110-
/// <param name="contextEnvName">Provide the environemnt name (development/staging/production). You can also pass in a custom name. Defaults to reading "trakerr.contextEnvName" property under appSettings from the App.config</param>
111-
public TrakerrClient(string apiKey = null, string contextAppVersion = null, string contextEnvName = "development")
118+
/// <param name="contextDeploymentStage">Provide the string representation of the deployment stage (development/staging/production). You can also pass in a custom name. Defaults to reading "trakerr.contextDeploymentStage" property under appSettings from the App.config</param>
119+
/// <param name="contextEnvLanguage">String representation of the language being used. If not provided defaults to C#, but can be passed other items in a string like "VB" or "Managed C++"</param>
120+
public TrakerrClient(string apiKey = null, string contextAppVersion = null, string contextDeploymentStage = null, string contextEnvLanguage = "C#")
112121
{
113122
if (apiKey == null) apiKey = ConfigurationManager.AppSettings["trakerr.apiKey"];
114123
if (contextAppVersion == null) contextAppVersion = ConfigurationManager.AppSettings["trakerr.contextAppVersion"];
115-
if (contextEnvName == null) contextEnvName = ConfigurationManager.AppSettings["trakerr.contextEnvName"];
124+
if (contextDeploymentStage == null) contextDeploymentStage = ConfigurationManager.AppSettings["trakerr.eploymentStage"];
116125

117126
this.apiKey = apiKey;
118127
this.contextAppVersion = contextAppVersion;
128+
this.contextDeploymentStage = contextDeploymentStage;
119129

120-
this.contextEnvName = contextEnvName;
121-
this.ContextEnvVersion = Type.GetType("Mono.Runtime") != null ? "Mono" : "Microsoft CLI";
130+
this.contextEnvLanguage = contextEnvLanguage;
131+
this.contextEnvName = Type.GetType("Mono.Runtime") != null ? "Mono" : "Microsoft CLI";
132+
133+
this.contextEnvVersion = null;
134+
Type type = Type.GetType("Mono.Runtime");
135+
if (type != null)
136+
{
137+
MethodInfo displayName = type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static);
138+
if (displayName != null)
139+
this.contextEnvVersion = displayName.Invoke(null, null).ToString();
140+
}
141+
else
142+
{
143+
try//Code might not work on non-windows.
144+
{
145+
this.contextEnvVersion = TrakerrClient.Get45PlusFromRegistry();
146+
}
147+
catch
148+
{
149+
this.contextEnvVersion = null;
150+
}
151+
}
152+
122153

123154
//Refactor 2 will push what is now contextEnvVersion to contextEnvName
124155
//To get the version then, follow: https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx for
125156
//Microsoft CLI and (probably) Enviroment.Version for Mono
126157

127-
if (ContextEnvHostname == null)
158+
if (contextEnvHostname == null)
128159
{
129160
try
130161
{
131-
this.ContextEnvHostname = Dns.GetHostName();
162+
this.contextEnvHostname = Dns.GetHostName();
132163
}
133164
catch(SocketException)
134165
{
135-
this.ContextEnvHostname = Environment.MachineName;
166+
this.contextEnvHostname = Environment.MachineName;
136167
}
137168
}
138169
else
139170
{
140-
this.ContextEnvHostname = ContextEnvHostname;
171+
this.contextEnvHostname = contextEnvHostname;
141172
}
142173

143-
this.ContextAppOS = ContextAppOS == null ? Environment.OSVersion.Platform + " " + Environment.OSVersion.ServicePack : ContextAppOS;
144-
this.ContextAppOSVersion = ContextAppOSVersion == null ? Environment.OSVersion.Version.ToString() : ContextAppOSVersion;
174+
this.contextAppOS = contextAppOS == null ? Environment.OSVersion.Platform + " " + Environment.OSVersion.ServicePack : contextAppOS;
175+
this.contextAppOSVersion = contextAppOSVersion == null ? Environment.OSVersion.Version.ToString() : contextAppOSVersion;
145176

146177
eventsApi = new EventsApi(ConfigurationManager.AppSettings["trakerr.url"]);
147178
}
148179

149180
/// <summary>
150-
/// Use this to bootstrap a new AppEvent object with the supplied classification, event type and message.
181+
/// Use this to bootstrap a new AppEvent object with the supplied logLevel, classification, event type and message.
151182
/// </summary>
152-
/// <param name="classification">Classification (Error/Warning/Info/Debug or custom string), defaults to "Error".</param>
183+
/// <param name="logLevel">String representation of the level (Error/Warning/Info/Debug) of the error, defaults to "Error" if null or passed in something else.</param>
184+
/// <param name="classification">Optional extra string descriptor. Defaults to issue.</param>
153185
/// <param name="eventType">Type of event (eg. System.Exception), defaults to "unknonwn"</param>
154186
/// <param name="eventMessage">Message, defaults to "unknown"</param>
155187
/// <returns>Newly created AppEvent</returns>
156-
public AppEvent CreateAppEvent(string classification = "Error", string eventType = "unknown", string eventMessage = "unknown")
157-
{
158-
return new AppEvent(this.apiKey, classification, eventType, eventMessage);
188+
public AppEvent CreateAppEvent(AppEvent.LogLevelEnum logLevel = AppEvent.LogLevelEnum.Error, string classification = "issue", string eventType = "unknown", string eventMessage = "unknown")
189+
{
190+
return new AppEvent(this.apiKey, logLevel, classification, eventType, eventMessage);
159191
}
160192

161193
/// <summary>
162194
/// Use this to bootstrap a new AppEvent object from an e.
163195
/// </summary>
164196
/// <param name="exception">The Exception to use to create the new AppEvent</param>
197+
/// <param name="classification">Optional extra string descriptor. Defaults to issue.</param>
165198
/// <returns>Newly created AppEvent</returns>
166-
public AppEvent CreateAppEventFromException(string classification, Exception exception)
199+
public AppEvent CreateAppEvent(Exception exception, string classification = "issue")
167200
{
168-
var exceptionEvent = CreateAppEvent(classification, exception.GetType().ToString(), exception.Message);
201+
var exceptionEvent = CreateAppEvent(classification: classification, eventType: exception.GetType().ToString(), eventMessage: exception.Message);
169202

170203
exceptionEvent.EventStacktrace = EventTraceBuilder.GetEventTraces(exception);
171204

@@ -181,7 +214,9 @@ public void SendEvent(AppEvent appEvent)
181214
// fill defaults if not overridden in the AppEvent being passed
182215
FillDefaults(appEvent);
183216

184-
eventsApi.EventsPost(appEvent);
217+
var response = eventsApi.EventsPostWithHttpInfo(appEvent);
218+
Console.Error.WriteLine("Status Code:" + response.StatusCode);
219+
Console.Error.WriteLine(response.Data);
185220
}
186221

187222
/// <summary>
@@ -193,7 +228,9 @@ public async void SendEventAsync(AppEvent appEvent)
193228
// fill defaults if not overridden in the AppEvent being passed
194229
FillDefaults(appEvent);
195230

196-
await eventsApi.EventsPostAsync(appEvent);
231+
var response = await eventsApi.EventsPostAsyncWithHttpInfo(appEvent);
232+
await Console.Error.WriteLineAsync("Status Code:" + response.StatusCode);
233+
await Console.Error.WriteLineAsync(response.Data.ToString());
197234
}
198235

199236

@@ -204,27 +241,74 @@ public async void SendEventAsync(AppEvent appEvent)
204241
private void FillDefaults(AppEvent appEvent)
205242
{
206243
if (appEvent.ApiKey == null) appEvent.ApiKey = apiKey;
207-
208244
if (appEvent.ContextAppVersion == null) appEvent.ContextAppVersion = contextAppVersion;
245+
appEvent.DeploymentStage = appEvent.DeploymentStage == null ? this.contextDeploymentStage : appEvent.DeploymentStage;
209246

247+
appEvent.ContextEnvLanguage = appEvent.ContextEnvLanguage == null ? this.contextEnvLanguage : appEvent.ContextEnvLanguage;
210248
if (appEvent.ContextEnvName == null) appEvent.ContextEnvName = this.contextEnvName;
211-
if (appEvent.ContextEnvVersion == null) appEvent.ContextEnvVersion = this.ContextEnvVersion;
212-
if (appEvent.ContextEnvHostname == null) appEvent.ContextEnvHostname = this.ContextEnvHostname;
249+
if (appEvent.ContextEnvVersion == null) appEvent.ContextEnvVersion = this.contextEnvVersion;
250+
if (appEvent.ContextEnvHostname == null) appEvent.ContextEnvHostname = this.contextEnvHostname;
213251

214252
if (appEvent.ContextAppOS == null)
215253
{
216-
appEvent.ContextAppOS = this.ContextAppOS;
217-
appEvent.ContextAppOSVersion = this.ContextAppOSVersion;
254+
appEvent.ContextAppOS = this.contextAppOS;
255+
appEvent.ContextAppOSVersion = this.contextAppOSVersion;
218256
}
219257

220-
appEvent.ContextAppBrowser = appEvent.ContextAppBrowser == null ? this.ContextAppOSBrowser : appEvent.ContextAppBrowser;
221-
appEvent.ContextAppBrowserVersion = appEvent.ContextAppBrowserVersion == null ? this.ContextAppOSBrowserVersion : appEvent.ContextAppBrowserVersion;
258+
appEvent.ContextAppBrowser = appEvent.ContextAppBrowser == null ? this.contextAppOSBrowser : appEvent.ContextAppBrowser;
259+
appEvent.ContextAppBrowserVersion = appEvent.ContextAppBrowserVersion == null ? this.contextAppOSBrowserVersion : appEvent.ContextAppBrowserVersion;
222260

223-
if (appEvent.ContextDataCenter == null) appEvent.ContextDataCenter = ContextDataCenter;
224-
if (appEvent.ContextDataCenterRegion == null) appEvent.ContextDataCenterRegion = ContextDataCenterRegion;
261+
if (appEvent.ContextDataCenter == null) appEvent.ContextDataCenter = contextDataCenter;
262+
if (appEvent.ContextDataCenterRegion == null) appEvent.ContextDataCenterRegion = contextDataCenterRegion;
225263

226264
if (!appEvent.EventTime.HasValue) appEvent.EventTime = (long)(DateTime.Now - DT_EPOCH).TotalMilliseconds;
227265
}
228266

267+
private static string Get45PlusFromRegistry()
268+
{
269+
const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
270+
using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
271+
{
272+
if (ndpKey != null && ndpKey.GetValue("Release") != null)
273+
{
274+
return CheckFor45PlusVersion((int)ndpKey.GetValue("Release"));
275+
}
276+
else
277+
{
278+
return System.Environment.Version.ToString();
279+
}
280+
}
281+
}
282+
283+
// Checking the version using >= will enable forward compatibility.
284+
private static string CheckFor45PlusVersion(int releaseKey)
285+
{
286+
if (releaseKey >= 394802)
287+
return "4.6.2 or later";
288+
if (releaseKey >= 394254)
289+
{
290+
return "4.6.1";
291+
}
292+
if (releaseKey >= 393295)
293+
{
294+
return "4.6";
295+
}
296+
if ((releaseKey >= 379893))
297+
{
298+
return "4.5.2";
299+
}
300+
if ((releaseKey >= 378675))
301+
{
302+
return "4.5.1";
303+
}
304+
if ((releaseKey >= 378389))
305+
{
306+
return "4.5";
307+
}
308+
// This code should never execute. A non-null release key should mean
309+
// that 4.5 or later is installed.
310+
return "No 4.5 or later version detected";
311+
}
312+
229313
}
230314
}

TrakerrSampleApp/App.config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
55
</startup>
66
<appSettings>
7-
<add key="trakerr.apiKey" value="your API Key Here" />
7+
<add key="trakerr.apiKey" value="b224233d1398d73d068f10a3a485060719967512097450" />
88
<add key="trakerr.url" value="https://www.trakerr.io/api/v1" />
99
<add key="trakerr.contextAppVersion" value="1.0" />
10-
<add key="trakerr.contextEnvName" value="development"/>
10+
<add key="trakerr.deploymentStage" value="development"/>
1111
</appSettings>
1212
</configuration>

TrakerrSampleApp/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ static void Main(string[] args)
2222
// Send the event to Trakerr
2323
e.SendToTrakerr();
2424
}
25+
Console.Out.WriteLine("Done!");
26+
Console.In.ReadLine();//Give time for the Async tasks to print to console.
2527
}
2628
}
2729
}

generated/src/IO.Trakerr/Api/EventsApi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public ApiResponse<Object> EventsPostWithHttpInfo (AppEvent data)
265265

266266
return new ApiResponse<Object>(localVarStatusCode,
267267
localVarResponse.Headers.ToDictionary(x => x.Name, x => x.Value.ToString()),
268-
null);
268+
localVarResponse.StatusDescription);
269269
}
270270

271271
/// <summary>
@@ -342,7 +342,7 @@ public async System.Threading.Tasks.Task<ApiResponse<Object>> EventsPostAsyncWit
342342

343343
return new ApiResponse<Object>(localVarStatusCode,
344344
localVarResponse.Headers.ToDictionary(x => x.Name, x => x.Value.ToString()),
345-
null);
345+
localVarResponse.StatusDescription);
346346
}
347347

348348
}

0 commit comments

Comments
 (0)