Created
February 13, 2017 21:26
-
-
Save altrive/8528d4ef593e628172bf17c090c84b8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Query Kind="Program"> | |
<NuGetReference>Newtonsoft.Json</NuGetReference> | |
<NuGetReference Version="4.5.0-alpha01" Prerelease="true">NLog</NuGetReference> | |
<Namespace>NLog</Namespace> | |
<Namespace>NLog.Config</Namespace> | |
<Namespace>NLog.Targets</Namespace> | |
<Namespace>Newtonsoft.Json</Namespace> | |
</Query> | |
void Main() | |
{ | |
//Setup NLog configurations | |
Init(); | |
//Write Logs inside methods | |
SampleMethod( | |
new InputData { AAA = "abc", BBB = 300, CCC = 0.123f }, | |
new InputData { } | |
); | |
} | |
private static NLog.ILogger logger; | |
public static void Init() | |
{ | |
var config = new LoggingConfiguration(); | |
var consoleTarget = new ConsoleTarget | |
{ | |
Layout = @"${longdate} [${uppercase:${level:padding=-5}}] ${message}", | |
}; | |
config.AddTarget("console", consoleTarget); | |
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, consoleTarget)); | |
LogManager.Configuration = config; | |
logger = NLog.LogManager.GetCurrentClassLogger(); | |
} | |
public OutputData SampleMethod(InputData data1, InputData data2) | |
{ | |
//Write method start logs | |
logger.Info("Start method: {method}...", nameof(SampleMethod)); | |
logger.Debug(" InputArgs: {Args}", new { data1, data2 }); | |
//Get test data | |
var sw = Stopwatch.StartNew(); | |
var result = GetData(); | |
Thread.Sleep(123); | |
//Write method completion logs | |
logger.Debug(" Results: {Result}", result); | |
logger.Info("Completed method: Elapsed {elepsed} [ms]", sw.ElapsedMilliseconds); | |
return result; | |
} | |
public OutputData GetData() | |
{ | |
return new OutputData | |
{ | |
AAA = "1", | |
BBB = 2, | |
CCC = 3.456f | |
}; | |
} | |
#region Sample Models | |
public class BaseModel | |
{ | |
public override string ToString() | |
{ | |
//Use Json.NET serializer | |
return JsonConvert.SerializeObject(this); | |
//Use NLog Json serializer | |
//return DefaultJsonSerializer.Instance.SerializeObject(this); | |
} | |
} | |
public class InputData : BaseModel | |
{ | |
public string AAA { get; set; } | |
public int BBB { get; set; } | |
public float CCC { get; set; } | |
} | |
public class OutputData : BaseModel | |
{ | |
public string AAA { get; set; } | |
public int BBB { get; set; } | |
public float CCC { get; set; } | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment