|
1 |
| -# gravity-abstraction-logging |
| 1 | +# Gravity Abstraction Logging |
2 | 2 | Gravity API, logging abstraction. Allows to create any type of logging output using standard conventions.
|
| 3 | + |
| 4 | +# Quick Start (using the existing ```TraceLogger```) |
| 5 | +```csharp |
| 6 | +/* |
| 7 | + * if you want the log files to be save in the current directory, do not set "inDirectory" argument. |
| 8 | + */ |
| 9 | + |
| 10 | +var logger = new TraceLogger(applicationName: "myApplication", loggerName: "DataClass", inDirectory: @"C:\Logs"); |
| 11 | +logger.Debug(message: "This is a debug message"); |
| 12 | +``` |
| 13 | + |
| 14 | +# Implement Your Own Message Handling |
| 15 | +```csharp |
| 16 | +/* |
| 17 | + * You must inherit Logger class. |
| 18 | + */ |
| 19 | + |
| 20 | +public class HttpLogger : Logger |
| 21 | +{ |
| 22 | + private readonly string baseAddress; |
| 23 | + private readonly string applicationName; |
| 24 | + private static readonly HttpClient client = new HttpClient(); |
| 25 | + |
| 26 | + // Will be used to pass values into base class. |
| 27 | + public HttpLogger(string applicationName, string loggerName) |
| 28 | + : this(applicationName, loggerName, baseAddress: "http://localhost:5000/") |
| 29 | + { } |
| 30 | + |
| 31 | + // Custom constructor to take some HTTP Context. |
| 32 | + public HttpLogger(string applicationName, string loggerName, string baseAddress) |
| 33 | + : base(applicationName, loggerName) |
| 34 | + { |
| 35 | + this.baseAddress = baseAddress; |
| 36 | + this.applicationName = applicationName; |
| 37 | + client.BaseAddress = new Uri(baseAddress); |
| 38 | + } |
| 39 | + |
| 40 | + // Creates a child logger with the same application name. |
| 41 | + public override ILogger CreateChildLogger(string loggerName) |
| 42 | + { |
| 43 | + return new HttpLogger(applicationName, loggerName, baseAddress); |
| 44 | + } |
| 45 | + |
| 46 | + // Override the message parsing and sending. |
| 47 | + // At this point the logMessage is already populated and you can change it before sending. |
| 48 | + public override void OnExecutor(string level, IDictionary<string, object> logMessage) |
| 49 | + { |
| 50 | + // setup |
| 51 | + var requestBody = JsonConvert.SerializeObject(logMessage); |
| 52 | + var content = new StringContent(content: requestBody, Encoding.UTF8, mediaType: "application/json"); |
| 53 | + |
| 54 | + // some message manipulation |
| 55 | + logMessage["EndPoint"] = $"{baseAddress}/logging"; |
| 56 | + logMessage["LogType"] = nameof(HttpLogger); |
| 57 | + |
| 58 | + // send |
| 59 | + client.PostAsync("/loggingServe", content).GetAwaiter().GetResult(); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/* |
| 64 | + * if you want the log files to be sent to "http://localhost:5000", do not set "baseAddress" argument. |
| 65 | + */ |
| 66 | + |
| 67 | +var logger = new HttpLogger(applicationName: "myApplication", loggerName: "DataClass", baseAddress: @"http://loggingserver/"); |
| 68 | +logger.Debug(message: "This is a debug message"); |
| 69 | +``` |
0 commit comments