public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter) { if (!IsEnabled(logLevel)) { return; } var formatted = formatter?.Invoke(state, exception) ?? exception?.ToString() ?? state?.ToString() ?? eventId.ToString(); AndroidLog.WriteLine(logLevel.ToLogPriority(), Tag, formatted); }
private void WriteLine(object message, LogPriority priority) { var trace = new StackTrace().GetFrame(2).GetMethod(); var callerMethod = trace.DeclaringType.FullName; Log.WriteLine(priority, callerMethod, message.ToString()); string logHeader = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss,fff} " + $"{priority.ToString().ToUpper()} {callerMethod}"; string logMessage = message.ToString(); lock (_lock) { using StreamWriter logWritter = new StreamWriter(CurrentLogPath, true, Encoding.UTF8); logWritter.WriteLine(logHeader); logWritter.WriteLine($" {logMessage}"); logWritter.WriteLine(); } }
/// <summary> /// Emit the provided log event to the sink. /// </summary> /// <param name="logEvent">The log event to write.</param> public void Emit(LogEvent logEvent) { if (logEvent == null) { throw new ArgumentNullException("logEvent"); } var renderSpace = new StringWriter(); _textFormatter.Format(logEvent, renderSpace); var tag = logEvent.Properties.Where(x => x.Key == Constants.SourceContextPropertyName).Select(x => x.Value.ToString("l", null)).FirstOrDefault() ?? ""; switch (logEvent.Level) { case LogEventLevel.Debug: AndroidLog.Debug(tag, renderSpace.ToString()); break; case LogEventLevel.Information: AndroidLog.Info(tag, renderSpace.ToString()); break; case LogEventLevel.Verbose: AndroidLog.Verbose(tag, renderSpace.ToString()); break; case LogEventLevel.Warning: AndroidLog.Warn(tag, renderSpace.ToString()); break; case LogEventLevel.Error: AndroidLog.Error(tag, renderSpace.ToString()); break; case LogEventLevel.Fatal: AndroidLog.Wtf(tag, renderSpace.ToString()); break; default: AndroidLog.WriteLine(LogPriority.Assert, tag, renderSpace.ToString()); break; } }