This repository has been archived by the owner on Jun 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Global.asax.cs
94 lines (76 loc) · 4.07 KB
/
Global.asax.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Logging.Log;
using Logging.Metrics;
namespace SampleSiteWithLogging {
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication {
public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start() {
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
ILogProvider provider = GetProviderFromSettings();
Logger.SetDefaultLogger(provider);
Logger.Log(new Dictionary<string, string>() { { "Type", "ApplicationStartup" }, { "Time", DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ") } }, null);
IMetricProvider mprovider = GetMetricProviderFromSettings();
MetricStore.SetDefaultMetrics(mprovider);
MetricStore.Store("ApplicationStartup");
}
protected void Application_BeginRequest() {
Logger.Log(new Dictionary<string, string>() { { "Type", "ApplicationRequest" }, { "UserAgent", Request.UserAgent }, { "Time", DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ") } }, null);
}
protected void Application_Error(Object sender, System.EventArgs e) {
System.Web.HttpContext context = HttpContext.Current;
System.Exception exc = Context.Server.GetLastError();
Logger.Log(new Dictionary<string, string>() { { "Type", "ApplicationError" }, { "Exception", exc.ToString() }, { "Time", DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ") } }, null);
}
protected ILogProvider GetProviderFromSettings() {
SensitiveSettings.SettingsManager.LoadFrom(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @"bin\"));
string provider = "null";
if (SensitiveSettings.SettingsManager.Settings.ContainsKey("LogProvider")) {
provider = SensitiveSettings.SettingsManager.Settings["LogProvider"];
}
switch (provider.ToUpper()) {
case "LOGGLY":
return new LogglyProvider(SensitiveSettings.SettingsManager.Settings["Loggly.BaseURL"]);
case "STORM":
return new StormProvider(SensitiveSettings.SettingsManager.Settings["Storm.BaseURL"], SensitiveSettings.SettingsManager.Settings["Storm.AccessToken"], SensitiveSettings.SettingsManager.Settings["Storm.ProjectId"]);
case "LOGENTRIES":
return new LogentriesProvider(SensitiveSettings.SettingsManager.Settings["logentries.BaseURL"], SensitiveSettings.SettingsManager.Settings["logentries.AccountKey"], SensitiveSettings.SettingsManager.Settings["logentries.Host"], SensitiveSettings.SettingsManager.Settings["logentries.Log"]);
case "SCALYR":
return new ScalyrProvider(SensitiveSettings.SettingsManager.Settings["Scalyr.BaseURL"], SensitiveSettings.SettingsManager.Settings["Scalyr.WriteLogsToken"]);
default:
return new NullLogProvider();
}
}
protected IMetricProvider GetMetricProviderFromSettings() {
SensitiveSettings.SettingsManager.LoadFrom(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @"bin\"));
string provider = "null";
if (SensitiveSettings.SettingsManager.Settings.ContainsKey("MetricProvider")) {
provider = SensitiveSettings.SettingsManager.Settings["MetricProvider"];
}
switch (provider.ToUpper()) {
case "LIBRATO":
return new LibratoMetricsProvider(SensitiveSettings.SettingsManager.Settings["Librato.URL"], SensitiveSettings.SettingsManager.Settings["Librato.Email"], SensitiveSettings.SettingsManager.Settings["Librato.Token"], true);
default:
return new NullMetricProvider();
}
}
}
}