internal static string ToFileLogUsing(this LogArguments args, IConfiguration configuration) { var fileConfiguration = configuration as IFormatConfiguration; return(args.ToFormatedString( fileConfiguration.FormatProvider, fileConfiguration.Format )); }
private static string ToFormatedString(this LogArguments args, IFormatProvider formatProvider, string format) => string.Format( formatProvider, format, args.Level.ToString(), DateTime.Now, args.ClassName, args.MethodName, args.Message, args.Exception ).Trim();
internal static Log ToDatabaseLogUsing(this LogArguments args, IConfiguration configuration) => new Log { Id = $"{Guid.NewGuid()}", Level = configuration.ShowLevel.Equals(Yes) ? args.Level.ToString() : null, Message = args.Message, Date = GetFormattedDateFrom(configuration.DateTypeFormat), Class = Path.GetFileNameWithoutExtension(args.ClassName), Method = args.MethodName, Exception = args.Exception?.ToString() ?? null };
private bool ShouldAdd(LogArguments logArguments, Func <LogArguments, IConfiguration, TLog> toLog) { if (!logArguments.IsLevelAllowed(configuration.Level)) { return(false); } var key = $"{DateTime.Now.ToString(AsKey)}-{Guid.NewGuid()}"; var log = toLog(logArguments, configuration); lock (padlock) logs.Add(key, log); return(true); }
public void Write( LogArguments logArguments, Func <LogArguments, IConfiguration, TLog> toLog, Func <KeyValuePair <string, TLog>, TLog> selector) { if (!ShouldAdd(logArguments, toLog)) { return; } if (IsUnderThreshold()) { return; } Write(selector); }
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext finCtx) { log.LogInformation("C# HTTP trigger function processed a request."); LogArguments testArg = new LogArguments(log, LoggerApp.ThisApp, finCtx.InvocationId.ToString(), finCtx.FunctionName.ToString()); FinLogger.Log("Test Error", LoggerLevel.Error, testArg); FinLogger.Log("Test Information ", LoggerLevel.Info, testArg); FinLogger.Log("Test Success", LoggerLevel.Success, testArg); FinLogger.Log("Test Trace", LoggerLevel.Trace, testArg); string name = req.Query["name"]; string requestBody = new StreamReader(req.Body).ReadToEnd(); dynamic data = JsonConvert.DeserializeObject(requestBody); name = name ?? data?.name; return(name != null ? (ActionResult) new OkObjectResult($"Hello, {name}") : new BadRequestObjectResult("Please pass a name on the query string or in the request body")); }
private void WriteToConsole(LogArguments logArguments) => logBuffer .Write( logArguments, (logArguments, configuration) => logArguments.ToConsoleLogUsing(configuration), kv => kv.Value );
private void WriteToFile(LogArguments logArguments) => logBuffer .Write( logArguments, (logArguments, configuration) => logArguments.ToFileLogUsing(configuration), kv => $"{kv.Value}{Environment.NewLine}" );
private void WriteToConsole(LogArguments logArguments) => logBuffer .Check(logArguments.IsLevelAllowed(configuration.Level)) ?.Add(logArguments.ToStringLogUsing(configuration)) .Validate(configuration.BufferSize) ?.Write(ConsoleBulkWriter.ToConsole, kv => kv.Value);
private void WriteToFile(LogArguments logArguments) => logBuffer .Check(logArguments.IsLevelAllowed(configuration.Level)) ?.Add(logArguments.ToStringLogUsing(configuration)) .Validate(configuration.BufferSize) ?.Write(FileBulkWriter.ToFileAsync, kv => $"{kv.Value}{Environment.NewLine}");
private void WriteToDatabase(LogArguments logArguments) => logBuffer .Check(logArguments.IsLevelAllowed(configuration.Level)) ?.Add(logArguments.ToDatabaseLogUsing(configuration)) .Validate(configuration.BufferSize) ?.Write(DatabaseBulkWriter.ToDatabaseAsync, kv => kv.Value);
internal static bool IsLevelAllowed(this LogArguments args, Level level) => level <= args.Level;