Skip to content

Commit acfe8a7

Browse files
author
mwatson
committed
Port to CLR core
1 parent 0cf8309 commit acfe8a7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+15127
-0
lines changed

CoreSrc/.vs/restore.dg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#:C:\BitBucket\stackify-api-dotnet\CoreSrc\StackifyLib\StackifyLib.xproj

CoreSrc/StackifyLib.sln

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25123.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "StackifyLib", "StackifyLib\StackifyLib.xproj", "{E1A20789-60A0-4C04-8FD8-C6213329CA2C}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{E1A20789-60A0-4C04-8FD8-C6213329CA2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{E1A20789-60A0-4C04-8FD8-C6213329CA2C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{E1A20789-60A0-4C04-8FD8-C6213329CA2C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{E1A20789-60A0-4C04-8FD8-C6213329CA2C}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

CoreSrc/StackifyLib/API.cs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net;
5+
using System.Text;
6+
using Newtonsoft.Json;
7+
using StackifyLib.Utils;
8+
9+
namespace StackifyLib
10+
{
11+
public class API
12+
{
13+
private static Utils.HttpClient client;
14+
static API()
15+
{
16+
client = new HttpClient(null, null);
17+
}
18+
19+
public static Models.Device[] GetDeviceList()
20+
{
21+
22+
var response = client.SendJsonAndGetResponse((client.BaseAPIUrl) +
23+
"API/Device/GetAll",
24+
null);
25+
26+
if (response.StatusCode == HttpStatusCode.OK)
27+
{
28+
return JsonConvert.DeserializeObject<Models.Device[]>(response.ResponseText);
29+
}
30+
else
31+
{
32+
return null;
33+
}
34+
}
35+
36+
public static bool RemoveServerByID(int id, bool uninstallAgent = false)
37+
{
38+
var response = client.POSTAndGetResponse((client.BaseAPIUrl) +
39+
"API/Device/RemoveServerByID/?id=" + id + "&uninstallAgent=" + uninstallAgent, null);
40+
41+
return response.StatusCode == HttpStatusCode.OK;
42+
}
43+
44+
public static bool RemoveServerByName(string name, bool uninstallAgent = false)
45+
{
46+
var response = client.POSTAndGetResponse((client.BaseAPIUrl) +
47+
"API/Device/RemoveServerByName/?name=" + name + "&uninstallAgent=" + uninstallAgent,
48+
null);
49+
50+
return response.StatusCode == HttpStatusCode.OK;
51+
}
52+
53+
public static bool UninstallAgentByID(int id)
54+
{
55+
var response = client.POSTAndGetResponse((client.BaseAPIUrl) +
56+
"API/Device/UninstallAgentByID/?id=" + id,
57+
null);
58+
59+
return response.StatusCode == HttpStatusCode.OK;
60+
}
61+
62+
public static bool UninstallAgentByName(string name)
63+
{
64+
var response = client.POSTAndGetResponse((client.BaseAPIUrl) +
65+
"API/Device/UninstallAgentByName/?name=" + name,
66+
null);
67+
68+
return response.StatusCode == HttpStatusCode.OK;
69+
}
70+
71+
public static bool? ConfigureAlerts(Dictionary<int, string> devices)
72+
{
73+
var response = client.SendJsonAndGetResponse((client.BaseAPIUrl) +
74+
"API/Device/ConfigureAlerts",
75+
JsonConvert.SerializeObject(devices));
76+
77+
if (response.StatusCode == HttpStatusCode.OK)
78+
{
79+
return JsonConvert.DeserializeObject<bool>(response.ResponseText);
80+
}
81+
else
82+
{
83+
return null;
84+
}
85+
}
86+
87+
public static Models.Monitor[] GetDeviceMonitors(int clientDeviceId)
88+
{
89+
var response = client.SendJsonAndGetResponse((client.BaseAPIUrl) +
90+
"API/Device/Monitors/" + clientDeviceId.ToString(),
91+
null);
92+
93+
if (response.StatusCode == HttpStatusCode.OK)
94+
{
95+
return JsonConvert.DeserializeObject<Models.Monitor[]>(response.ResponseText);
96+
}
97+
else
98+
{
99+
return null;
100+
}
101+
}
102+
103+
public static Models.MonitorMetric GetMetrics(int monitorID, DateTimeOffset startDateUtc, DateTimeOffset endDateUtc, short pointSizeInMinutes = 5)
104+
{
105+
var response = client.SendJsonAndGetResponse((client.BaseAPIUrl) +
106+
"API/Device/MonitorMetrics/" + string.Format("?monitorID={0}&startDateUtc={1}&endDateUtc={2}&pointSizeInMinutes={3}", monitorID, startDateUtc.UtcDateTime.ToString("o"), endDateUtc.UtcDateTime.ToString("o"), pointSizeInMinutes),
107+
null);
108+
109+
if (response.StatusCode == HttpStatusCode.OK)
110+
{
111+
return JsonConvert.DeserializeObject<Models.MonitorMetric>(response.ResponseText);
112+
}
113+
else
114+
{
115+
return null;
116+
}
117+
}
118+
}
119+
}

CoreSrc/StackifyLib/Config.cs

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Diagnostics;
6+
using Newtonsoft.Json;
7+
8+
namespace StackifyLib
9+
{
10+
/// <summary>
11+
/// Encapsulate settings retrieval mechanism. Currently supports config file and environment variables.
12+
/// Could be expanded to include other type of configuration servers later.
13+
/// </summary>
14+
public class Config
15+
{
16+
public static void LoadSettings()
17+
{
18+
try
19+
{
20+
CaptureErrorPostdata = Get("Stackify.CaptureErrorPostdata", "")
21+
.Equals("true", StringComparison.CurrentCultureIgnoreCase);
22+
23+
CaptureServerVariables = Get("Stackify.CaptureServerVariables", "")
24+
.Equals("true", StringComparison.CurrentCultureIgnoreCase);
25+
CaptureSessionVariables = Get("Stackify.CaptureSessionVariables", "")
26+
.Equals("true", StringComparison.CurrentCultureIgnoreCase);
27+
28+
CaptureErrorHeaders = Get("Stackify.CaptureErrorHeaders", "")
29+
.Equals("true", StringComparison.CurrentCultureIgnoreCase);
30+
31+
CaptureErrorCookies = Get("Stackify.CaptureErrorCookies", "")
32+
.Equals("true", StringComparison.CurrentCultureIgnoreCase);
33+
34+
CaptureErrorHeadersWhitelist = Get("Stackify.CaptureErrorHeadersWhitelist", "");
35+
36+
if (!string.IsNullOrEmpty(CaptureErrorHeadersWhitelist))
37+
{
38+
ErrorHeaderGoodKeys = CaptureErrorHeadersWhitelist.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
39+
}
40+
41+
CaptureErrorHeadersBlacklist = Get("Stackify.CaptureErrorHeadersBlacklist", "");
42+
if (!string.IsNullOrEmpty(CaptureErrorHeadersBlacklist))
43+
{
44+
ErrorHeaderBadKeys = CaptureErrorHeadersBlacklist.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
45+
}
46+
47+
CaptureErrorCookiesWhitelist = Get("Stackify.CaptureErrorCookiesWhitelist", "");
48+
if (!string.IsNullOrEmpty(CaptureErrorCookiesWhitelist))
49+
{
50+
ErrorCookiesGoodKeys = CaptureErrorCookiesWhitelist.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
51+
}
52+
53+
CaptureErrorCookiesBlacklist = Get("Stackify.CaptureErrorCookiesBlacklist", "");
54+
if (!string.IsNullOrEmpty(CaptureErrorCookiesBlacklist))
55+
{
56+
ErrorCookiesBadKeys = CaptureErrorCookiesBlacklist.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
57+
}
58+
59+
CaptureErrorSessionWhitelist = Get("Stackify.CaptureErrorSessionWhitelist", "");
60+
if (!string.IsNullOrEmpty(CaptureErrorSessionWhitelist))
61+
{
62+
ErrorSessionGoodKeys = CaptureErrorSessionWhitelist.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
63+
}
64+
65+
}
66+
catch (Exception ex)
67+
{
68+
Debug.WriteLine(ex.ToString());
69+
}
70+
}
71+
72+
public static List<string> ErrorHeaderGoodKeys = new List<string>();
73+
public static List<string> ErrorHeaderBadKeys = new List<string>();
74+
public static List<string> ErrorCookiesGoodKeys = new List<string>();
75+
public static List<string> ErrorCookiesBadKeys = new List<string>();
76+
public static List<string> ErrorSessionGoodKeys = new List<string>();
77+
78+
public static bool CaptureSessionVariables { get; set; } = false;
79+
public static bool CaptureServerVariables { get; set; } = false;
80+
public static bool CaptureErrorPostdata { get; set; } = false;
81+
public static bool CaptureErrorHeaders { get; set; } = true;
82+
public static bool CaptureErrorCookies { get; set; } = false;
83+
84+
public static string CaptureErrorSessionWhitelist { get; set; } = null;
85+
86+
public static string CaptureErrorHeadersWhitelist { get; set; } = null;
87+
88+
public static string CaptureErrorHeadersBlacklist { get; set; } = "cookie,authorization";
89+
90+
public static string CaptureErrorCookiesWhitelist { get; set; } = null;
91+
92+
public static string CaptureErrorCookiesBlacklist { get; set; } = ".ASPXAUTH";
93+
94+
95+
/// <summary>
96+
/// Attempts to fetch a setting value given the key.
97+
/// .NET configuration file will be used first, if the key is not found, environment variable will be used next.
98+
/// </summary>
99+
/// <param name="key">configuration key in config file or environment variable name.</param>
100+
/// <param name="defaultValue">If nothing is found, return optional defaultValue provided.</param>
101+
/// <returns>string value for the requested setting key.</returns>
102+
internal static string Get(string key, string defaultValue = null)
103+
{
104+
return defaultValue;
105+
//string v = null;
106+
//try
107+
//{
108+
// if (key != null)
109+
// {
110+
// v = ConfigurationManager.AppSettings[key];
111+
// if (string.IsNullOrEmpty(v))
112+
// v = Environment.GetEnvironmentVariable(key);
113+
// }
114+
//}
115+
//finally
116+
//{
117+
// if (v == null)
118+
// v = defaultValue;
119+
//}
120+
//return v;
121+
}
122+
}
123+
}

CoreSrc/StackifyLib/Extensions.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.ComponentModel;
5+
using System.Diagnostics;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace StackifyLib
11+
{
12+
public static class Extensions
13+
{
14+
public static void SendToStackify(this Exception ex)
15+
{
16+
try
17+
{
18+
StackifyLib.StackifyError.New(ex).SendToStackify();
19+
}
20+
catch (Exception e)
21+
{
22+
Debug.WriteLine("Error submitting error to Stackify " + e.ToString());
23+
throw;
24+
}
25+
}
26+
27+
public static StackifyLib.StackifyError NewStackifyError(this Exception ex)
28+
{
29+
try
30+
{
31+
return StackifyLib.StackifyError.New(ex);
32+
}
33+
catch (Exception e)
34+
{
35+
Debug.WriteLine("Error submitting error to Stackify " + e.ToString());
36+
throw;
37+
}
38+
}
39+
}
40+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using StackifyLib.Models;
2+
using StackifyLib.Utils;
3+
4+
namespace StackifyLib.Internal.Logs
5+
{
6+
public interface ILogClient
7+
{
8+
bool CanQueue();
9+
bool CanSend();
10+
bool CanUpload();
11+
void Close();
12+
bool ErrorShouldBeSent(StackifyError error);
13+
AppIdentityInfo GetIdentity();
14+
bool IsAuthorized();
15+
void PauseUpload(bool isPaused);
16+
void QueueMessage(LogMsg msg);
17+
}
18+
}

0 commit comments

Comments
 (0)