-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdvancedExample.cs
More file actions
133 lines (111 loc) · 5.77 KB
/
AdvancedExample.cs
File metadata and controls
133 lines (111 loc) · 5.77 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// Do this to easily use HypnoLog as HL in your code
using HL = HypnoLog.HypnoLog;
namespace HypnoLogExample
{
public static class AdvancedExample
{
public static void Run()
{
// HypnoLog C# Advanced usage examples:
// == error handling ==
// Note: this is an experimental feature
// You can subscribe to HypnoLog errors to provide your own way of handling such situations
// Note: it is best to provide an Error handling function before initializing HypnoLog,
// this will enable you to catch initialization errors as well.
// TODO: allow passing error-handling-function in initialization method as argument.
HL.ErrorOccurred += (obj, exception) =>
{
Console.WriteLine("Couldn't log using HypnoLog.\nObject: {0}\nError: {1}", obj, exception);
};
// == Initializing ==
// Note: this is an experimental feature
// Initialize the HypnoLog
// It is not mandatory to call Initialize but it is recommended to do so as soon as
// possible in the begging of the program. This will help marking the begging
// of new session in a proper way, by logging "new session" message.
// If Initialize was not invoked manually, it will be called implicitly at the first
// logging action.
HL.Initialize(
host: "localhost",
port: 7000,
shouldRedirect: false);
// Also you can call initialization without parameters. Default server will be used (http://localhost:7000/).
// Setting `shouldRedirect` as `true` equivalent of calling `RedirectConsoleOutput()`, see following.
// == Redirecting Console output ==
// Note: this is an experimental feature
// Calling `RedirectConsoleOutput()` will redirect all `System.Console` output to HypnoLog.
// This can be useful if u already have a program with a lot of output to the console.
//HL.RedirectConsoleOutput();
// == Logging ==
// Note: this is an experimental feature
// Example of logging a string with arguments which will be format,
// as you would do in `Console.WriteLine("x = {0}", x);`
HL.Log("This is logged form C# version {0} at {1}", Environment.Version , DateTime.Now);
// TODO: when logging `HL.Log("bal is {0} ", args: "sunny");` the `args` is required
// otherwise the method is ambiguous
//HL.Log("bal is {0} ", "sunny");
// == Tags ==
// Note: this is an experimental feature
// Example of logging with tags.
// To log with tags, add your tags with the `Tag()` method and then invoke the logging method of your choice.
// You can tag with multiple tags,
// tags can be separated by hash (`#`) or space(` `).
HL.Tag("#info").Log("Some data Tagged as info");
HL.Tag("#info #weather").Log("Weather will be sunny. Tagged as #info and as #weather");
var detailedWeatherDescription =
"Weather report received over network:\n" +
"Weather will be cool and cloudy\n" +
"Sun will be soft but warm\n" +
"Waves will be head hight \\m/\n";
// Combine tagging and named-logging
HL.Tag("info weather detailed").NamedLog(new { detailedWeatherDescription });
// Log some graph and tag it
HL.Tag("#info #numbers").Log(new[] { 1, 2, 3, 4, 5 }, type: "plot");
// == Named logging ==
// Note: this is an experimental feature
// Example of logging variable with its name
// This is good to avoid code like this:
// var x = GetSomeValue();
// Log("x: " + x);
// Then you change the name of 'x' to 'y' and the logging become misleading.
// Note: To log variable with it's name use the "NamedLog" function, and warp the
// variable with `new {}` deceleration.
HL.Log("Example of logging with variable name:");
var walter = "Also known as Mr.White";
HL.NamedLog(new {walter});
// == Synchronous usage ==
// Note: this is an experimental feature
// In case you want to use some logging methods from scopes do not allow multi-threading
// such as invoking method from Visual Studio Immediate Window,
// use the Synchronous version of the logging methods.
// Example of synchronous logging
HL.LogSync("Logging some string, synchronously!");
// == Watching ==
// Note: this is an experimental feature
// Not working right now
//// Example of watching a variable.
//// Note: To watch variable we need it's name and therefor we have to warp
//// the variable with new {} deceleration as we do in "NamedLog" function.
//var sky = "Blue clear sky";
//HL.Watch(new { sky });
//// Change the value and watch it changing.
//sky = "Gray foggy sky";
//HL.Watch(new { sky });
//// Example of watching two variables with the same name, in different scopes.
//CheckTheWeather();
}
/// <summary>
/// Example of watching a variable in another scope
/// </summary>
private static void CheckTheWeather()
{
var sky = "Blood moon night";
HL.Watch(new { sky });
}
}
}