|
| 1 | +using System; |
| 2 | + |
| 3 | +using NLog; |
| 4 | +using NLog.Config; |
| 5 | +using NLog.Targets; |
| 6 | + |
| 7 | +// ReSharper disable ConvertToConstant.Local |
| 8 | + |
| 9 | +namespace Sentry.Samples.NLog |
| 10 | +{ |
| 11 | + public static class Program |
| 12 | + { |
| 13 | + private static void Main(string[] args) |
| 14 | + { |
| 15 | + try |
| 16 | + { |
| 17 | + // You can configure your logger using a configuration file: |
| 18 | + UsingNLogConfigFile(); |
| 19 | + |
| 20 | + // Or you can configure it with code: |
| 21 | + UsingCodeConfiguration(); |
| 22 | + } |
| 23 | + catch (Exception e) |
| 24 | + { |
| 25 | + Console.WriteLine(e); |
| 26 | + } |
| 27 | + finally |
| 28 | + { |
| 29 | + LogManager.Shutdown(); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + private static void DemoLogger(ILogger logger) |
| 34 | + { |
| 35 | + // Minimum Breadcrumb and Event log levels are set to levels higher than Trace. |
| 36 | + // In this case, Trace messages are ignored |
| 37 | + logger.Trace("Verbose message which is not sent."); |
| 38 | + |
| 39 | + // Minimum Breadcrumb level is set to Debug so the following message is stored in memory and sent |
| 40 | + // with following events of the same Scope |
| 41 | + logger.Debug("Debug message stored as breadcrumb."); |
| 42 | + |
| 43 | + // Sends an event and stores the message as a breadcrumb too, to be sent with any upcoming events. |
| 44 | + logger.Error("Some event that includes the previous breadcrumbs. mood = {mood}", "happy that my error is reported"); |
| 45 | + |
| 46 | + try |
| 47 | + { |
| 48 | + DoWork(logger); |
| 49 | + } |
| 50 | + catch (Exception e) |
| 51 | + { |
| 52 | + e.Data.Add("details", "DoWork always throws."); |
| 53 | + logger.Fatal(e, "Error: with exception. {data}", new { title = "compound data object", wowFactor = 11, errorReported = true }); |
| 54 | + LogManager.Flush(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private static void DoWork(ILogger logger) |
| 59 | + { |
| 60 | + int a = 0, b = 10; |
| 61 | + |
| 62 | + logger.Info("Dividing {b} by {a}", b, a); |
| 63 | + |
| 64 | + logger.Warn("a is 0"); |
| 65 | + |
| 66 | + _ = 10 / a; |
| 67 | + } |
| 68 | + |
| 69 | + private static void UsingNLogConfigFile() |
| 70 | + { |
| 71 | + // If using an NLog.config xml file, NLog will load the configuration automatically Or, if using a |
| 72 | + // different file, you can call the following to load it for you: LogManager.Configuration = LogManager.LoadConfiguration("NLog.config").Configuration; |
| 73 | + |
| 74 | + var logger = LogManager.GetCurrentClassLogger(); |
| 75 | + DemoLogger(logger); |
| 76 | + } |
| 77 | + |
| 78 | + private static void UsingCodeConfiguration() |
| 79 | + { |
| 80 | + // Other overloads exist, for example, configure the SDK with only the DSN or no parameters at all. |
| 81 | + var config = LogManager.Configuration = new LoggingConfiguration(); |
| 82 | + config |
| 83 | + .AddSentry(o => |
| 84 | + { |
| 85 | + o.Layout = "${message}"; |
| 86 | + o.BreadcrumbLayout = "${logger}: ${message}"; // Optionally specify a separate format for breadcrumbs |
| 87 | + |
| 88 | + o.MinimumBreadcrumbLevel = LogLevel.Debug; // Debug and higher are stored as breadcrumbs (default is Info) |
| 89 | + o.MinimumEventLevel = LogLevel.Error; // Error and higher is sent as event (default is Error) |
| 90 | + |
| 91 | + // If DSN is not set, the SDK will look for an environment variable called SENTRY_DSN. If |
| 92 | + // nothing is found, SDK is disabled. |
| 93 | + o.Dsn = new Dsn("https://[email protected]/1188141"); |
| 94 | + |
| 95 | + o.AttachStacktrace = true; |
| 96 | + o.SendDefaultPii = true; // Send Personal Identifiable information like the username of the user logged in to the device |
| 97 | + |
| 98 | + o.IncludeEventDataOnBreadcrumbs = true; // Optionally include event properties with breadcrumbs |
| 99 | + o.ShutdownTimeoutSeconds = 5; |
| 100 | + |
| 101 | + o.AddTag("logger", "${logger}"); // Send the logger name as a tag |
| 102 | + |
| 103 | + // Other configuration |
| 104 | + }); |
| 105 | + |
| 106 | + config.AddTarget(new DebuggerTarget("debugger")); |
| 107 | + |
| 108 | + config.AddTarget(new ColoredConsoleTarget("console")); |
| 109 | + config.AddRuleForAllLevels("console"); |
| 110 | + config.AddRuleForAllLevels("debugger"); |
| 111 | + |
| 112 | + LogManager.Configuration = config; |
| 113 | + |
| 114 | + var Log = LogManager.GetCurrentClassLogger(); |
| 115 | + DemoLogger(Log); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments