Skip to content

Commit 6dcb838

Browse files
Merge pull request #86 from stackify/develop
Updating master with dev changes
2 parents 7049879 + 6e26ba0 commit 6dcb838

38 files changed

+489
-446
lines changed

README.md

Lines changed: 137 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Stackify API for .NET
22

3+
[![Build status](https://ci.appveyor.com/api/projects/status/6suxse470ab4bdxl?svg=true)](https://ci.appveyor.com/project/jaredcnance/stackify-api-dotnet)
4+
[![NuGet](https://img.shields.io/nuget/v/StackifyLib.svg)](https://www.nuget.org/packages/StackifyLib/)
5+
36
Library for Stackify users to integrate Stackify in to their projects. Provides support for sending errors, logs, and custom metrics to Stackify. Also some support for querying metric data back out of Stackify for use in external projects.
47

58
**Important links:**
@@ -38,41 +41,52 @@ The packages for log4net, NLog and Elmah all depend on StackifyLib. StackifyLib
3841

3942
The following is required in your App.config or Web.config:
4043

41-
<appSettings>
42-
<add key="Stackify.ApiKey" value="YOUR API KEY HERE" />
43-
<add key="Stackify.AppName" value="YOUR APP NAME" />
44-
<add key="Stackify.Environment" value="OPTIONAL ENVIRONMENT NAME LIKE PROD, DEV" />
45-
</appSettings>
44+
```xml
45+
<appSettings>
46+
<add key="Stackify.ApiKey" value="YOUR API KEY HERE" />
47+
<add key="Stackify.AppName" value="YOUR APP NAME" />
48+
<add key="Stackify.Environment" value="OPTIONAL ENVIRONMENT NAME LIKE PROD, DEV" />
49+
</appSettings>
50+
```
4651

4752
Optionally, you can set the config settings on your machine's environment variables with the same configuration key and value.
4853
Example are executed in window's cmd as an admin:
54+
```
55+
setx Stackify.ApiKey "YOUR API KEY HERE" /m
56+
setx Stackify.Environment "MY ENVIRONMENT HERE" /m
57+
```
4958

50-
setx Stackify.ApiKey "YOUR API KEY HERE" /m
51-
setx Stackify.Environment "MY ENVIRONMENT HERE" /m
52-
5359
You can set the config settings in code like so which will override the appSettings configs as well.
5460

55-
StackifyLib.Logger.GlobalApiKey = "";
56-
StackifyLib.Logger.GlobalAppName = "";
57-
StackifyLib.Logger.GlobalEnvironment = "";
58-
61+
```
62+
StackifyLib.Logger.GlobalApiKey = "";
63+
StackifyLib.Logger.GlobalAppName = "";
64+
StackifyLib.Logger.GlobalEnvironment = "";
65+
```
66+
5967
By default the library will use the WebRequest.DefaultWebProxy. If you want to set a specific proxy server only for StackifyLib, you can do so in code OR via config.
6068

61-
<appSettings>
62-
<add key="Stackify.ProxyServer" value="http://test:[email protected]:8888/" />
63-
</appSettings>
64-
65-
StackifyLib.Utils.HttpClient.CustomWebProxy = new WebProxy();
69+
```xml
70+
<appSettings>
71+
<add key="Stackify.ProxyServer" value="http://test:[email protected]:8888/" />
72+
</appSettings>
73+
```
74+
75+
```csharp
76+
StackifyLib.Utils.HttpClient.CustomWebProxy = new WebProxy();
77+
```
6678

6779
If you are having problems you can get logging out of the framework by hooking in to its custom logging.
6880

69-
StackifyLib.Utils.StackifyAPILogger.LogEnabled = true;
70-
StackifyLib.Utils.StackifyAPILogger.OnLogMessage += StackifyAPILogger_OnLogMessage;
81+
```csharp
82+
StackifyLib.Utils.StackifyAPILogger.LogEnabled = true;
83+
StackifyLib.Utils.StackifyAPILogger.OnLogMessage += StackifyAPILogger_OnLogMessage;
7184

72-
static void StackifyAPILogger_OnLogMessage(string data)
73-
{
74-
Debug.WriteLine(data);
75-
}
85+
static void StackifyAPILogger_OnLogMessage(string data)
86+
{
87+
Debug.WriteLine(data);
88+
}
89+
```
7690

7791

7892
## Errors and Logs
@@ -88,10 +102,10 @@ PM> Install-Package NLog.Targets.Stackify
88102
```
89103

90104
Sample config:
91-
92-
<nlog>
105+
```xml
106+
<nlog>
93107
<extensions>
94-
<add assembly="NLog.Targets.Stackify"/>
108+
<add assembly="NLog.Targets.Stackify"/>
95109
</extensions>
96110
<targets>
97111
<target name="stackify" type="StackifyTarget" logAllParams="false">
@@ -100,17 +114,20 @@ Sample config:
100114
</target>
101115
</targets>
102116
<rules>
103-
<logger name="*" writeTo="stackify" minlevel="Debug" />
117+
<logger name="*" writeTo="stackify" minlevel="Debug" />
104118
</rules>
105-
</nlog>
119+
</nlog>
120+
```
106121

107122
Logging custom objects is supported and will be searchable in Stackify's log viewer
108123

109-
static NLog.Logger nlog = NLog.LogManager.GetCurrentClassLogger();
110-
Dictionary<string, object> dictionary = new Dictionary<string, object>();
111-
dictionary["clientid"] = 1;
112-
dictionary["color"] = "red";
113-
nlog.Debug("Test message", dictionary);
124+
```csharp
125+
static NLog.Logger nlog = NLog.LogManager.GetCurrentClassLogger();
126+
Dictionary<string, object> dictionary = new Dictionary<string, object>();
127+
dictionary["clientid"] = 1;
128+
dictionary["color"] = "red";
129+
nlog.Debug("Test message", dictionary);
130+
```
114131

115132
Options:
116133

@@ -132,36 +149,39 @@ PM> Install-Package StackifyLib.log4net
132149

133150
Note: Nuget packages are compiled against 2.0.0 (1.2.11) but any newer version will work with a valid assembly binding redirect. log4net 2.0.3 is actually 1.2.13 which makes the binding redirect look strange.
134151

135-
<dependentAssembly>
152+
```xml
153+
<dependentAssembly>
136154
<assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" culture="neutral" />
137155
<bindingRedirect oldVersion="1.2.11.0-1.2.13.0" newVersion="1.2.13.0" />
138-
</dependentAssembly>
156+
</dependentAssembly>
157+
```
139158

140159
Sample config:
141-
142-
<log4net>
143-
<root>
144-
<level value="DEBUG" />
145-
<appender-ref ref="StackifyAppender" />
146-
</root>
147-
<appender name="StackifyAppender" type="StackifyLib.log4net.StackifyAppender, StackifyLib.log4net">
148-
<globalContextKeys>examplekey1,key2</globalContextKeys>
149-
<threadContextKeys></threadContextKeys>
150-
<logicalThreadContextKeys></logicalThreadContextKeys>
151-
152-
<callContextKeys></callContextKeys>
153-
154-
<!-- If logging a very high rate of messages, disable logging method names for performance -->
155-
<logMethodNames>true</logMethodNames>
156-
157-
<!-- Only log errors and fatals by using filters and setting levelMin and levelMax appropriately -->
158-
<!-- http://logging.apache.org/log4net/release/manual/configuration.html -->
159-
<filter type="log4net.Filter.LevelRangeFilter">
160-
<levelMin value="DEBUG" />
161-
<levelMax value="FATAL" />
162-
</filter>
163-
</appender>
164-
</log4net>
160+
```xml
161+
<log4net>
162+
<root>
163+
<level value="DEBUG" />
164+
<appender-ref ref="StackifyAppender" />
165+
</root>
166+
<appender name="StackifyAppender" type="StackifyLib.log4net.StackifyAppender, StackifyLib.log4net">
167+
<globalContextKeys>examplekey1,key2</globalContextKeys>
168+
<threadContextKeys></threadContextKeys>
169+
<logicalThreadContextKeys></logicalThreadContextKeys>
170+
171+
<callContextKeys></callContextKeys>
172+
173+
<!-- If logging a very high rate of messages, disable logging method names for performance -->
174+
<logMethodNames>true</logMethodNames>
175+
176+
<!-- Only log errors and fatals by using filters and setting levelMin and levelMax appropriately -->
177+
<!-- http://logging.apache.org/log4net/release/manual/configuration.html -->
178+
<filter type="log4net.Filter.LevelRangeFilter">
179+
<levelMin value="DEBUG" />
180+
<levelMax value="FATAL" />
181+
</filter>
182+
</appender>
183+
</log4net>
184+
```
165185

166186
Options
167187

@@ -174,14 +194,16 @@ Options
174194

175195
log4net does not internally have methods for logging a log message along with an object. Stackify's appenders work fine if you log an object directly or we have created some friendly extension methods to make it easy to log an object with your message at the same time.
176196

177-
using StackifyLib; //extension methods are here
178-
static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(Program));
179-
Dictionary<string, object> dictionary = new Dictionary<string, object>();
180-
dictionary["clientid"] = 1;
181-
dictionary["name"] = "process name";
182-
logger.Debug("Starting some process for client 1"); //Normal basic log message works fine
183-
logger.Debug(dictionary); //This works fine and is indexed and searchable by Stackify
184-
logger.Debug("Starting some process for client 1", dictionary); //extension method
197+
```cshapr
198+
using StackifyLib; //extension methods are here
199+
static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(Program));
200+
Dictionary<string, object> dictionary = new Dictionary<string, object>();
201+
dictionary["clientid"] = 1;
202+
dictionary["name"] = "process name";
203+
logger.Debug("Starting some process for client 1"); //Normal basic log message works fine
204+
logger.Debug(dictionary); //This works fine and is indexed and searchable by Stackify
205+
logger.Debug("Starting some process for client 1", dictionary); //extension method
206+
```
185207

186208
### log4net v1.2.10
187209

@@ -202,54 +224,58 @@ PM> Install-Package StackifyLib
202224

203225
If you use a custom logging framework or a framework not currently supported, you can easily send logs to Stackify with our core library and API like so:
204226

205-
StackifyLib.Logger.Queue("DEBUG", "My log message");
206-
StackifyLib.Logger.QueueException("Test exception", new ApplicationException("Sky is falling"));
207-
208-
StackifyLib.Logger.Shutdown(); //should be called before your app closes to flush the log queue
209-
210-
//More advanced example
211-
LogMsg msg = new LogMsg();
212-
msg.Ex = StackifyError.New(new ApplicationException("Exception goes here"));
213-
msg.AppDetails = new LogMsgGroup() {AppName = "My app", Env = "Prod", ServerName = Environment.MachineName};
214-
msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(new { color= "red"}, true);
215-
msg.Msg = "My log message";
216-
msg.Level = "ERROR";
217-
StackifyLib.Logger.QueueLogObject(msg);
227+
```csharp
228+
StackifyLib.Logger.Queue("DEBUG", "My log message");
229+
StackifyLib.Logger.QueueException("Test exception", new ApplicationException("Sky is falling"));
230+
231+
StackifyLib.Logger.Shutdown(); //should be called before your app closes to flush the log queue
232+
233+
//More advanced example
234+
LogMsg msg = new LogMsg();
235+
msg.Ex = StackifyError.New(new ApplicationException("Exception goes here"));
236+
msg.AppDetails = new LogMsgGroup() {AppName = "My app", Env = "Prod", ServerName = Environment.MachineName};
237+
msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(new { color= "red"}, true);
238+
msg.Msg = "My log message";
239+
msg.Level = "ERROR";
240+
StackifyLib.Logger.QueueLogObject(msg);
241+
```
218242

219243
*Make sure you call StackifyLib.Logger.Shutdown() before your app ends to flush the queue*
220244

221245
### Configuring with Azure service definitions
222246

223247
StackifyLib reads the license key, app name, and environment settings from normal web.config appSettings. If you would prefer to store the settings in an [azure cloud deployment cscfg](http://msdn.microsoft.com/en-us/library/azure/hh369931.aspx#NameValue), then you can create a little code to read the settings from there and set the StackifyLib settings in code like this in some similar way.
224248

225-
public class MvcApplication : System.Web.HttpApplication
226-
{
227-
public override void Init()
228-
{
229-
base.Init();
230-
StackifyLib.Logger.GlobalApiKey = GetConfig("Stackify.ApiKey");
231-
StackifyLib.Logger.GlobalEnvironment = GetConfig("Stackify.Environment");
232-
StackifyLib.Logger.GlobalAppName = "My App Name"; //probably no reason to make this one configurable
233-
}
234-
}
235-
236-
public static string GetConfig(string configName)
237-
{
238-
//Doing an if here in case it's being used outside of azure emulator.
239-
//Do this however works best for your project
240-
try
241-
{
242-
if (RoleEnvironment.IsAvailable)
243-
{
244-
return RoleEnvironment.GetConfigurationSettingValue(configName);
245-
}
246-
else
247-
{
248-
return ConfigurationManager.AppSettings[configName];
249-
}
250-
}
251-
catch (Exception ex)
252-
{
253-
}
254-
return null;
255-
}
249+
```csharp
250+
public class MvcApplication : System.Web.HttpApplication
251+
{
252+
public override void Init()
253+
{
254+
base.Init();
255+
StackifyLib.Logger.GlobalApiKey = GetConfig("Stackify.ApiKey");
256+
StackifyLib.Logger.GlobalEnvironment = GetConfig("Stackify.Environment");
257+
StackifyLib.Logger.GlobalAppName = "My App Name"; //probably no reason to make this one configurable
258+
}
259+
}
260+
261+
public static string GetConfig(string configName)
262+
{
263+
//Doing an if here in case it's being used outside of azure emulator.
264+
//Do this however works best for your project
265+
try
266+
{
267+
if (RoleEnvironment.IsAvailable)
268+
{
269+
return RoleEnvironment.GetConfigurationSettingValue(configName);
270+
}
271+
else
272+
{
273+
return ConfigurationManager.AppSettings[configName];
274+
}
275+
}
276+
catch (Exception ex)
277+
{
278+
}
279+
return null;
280+
}
281+
```

Src/ConsoleTest/App.config

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)