private LogEntry BuildLogEntry(LoggingEvent loggingEvent) { const string unknown = "[unknown]"; var labels = new Dictionary <string, string>(); LogEntrySourceLocation sourceLocation = null; if (_withMetaData.Contains(MetaDataType.Location)) { sourceLocation = BuildLogEntryLocation(loggingEvent); } if (_withMetaData.Contains(MetaDataType.Identity)) { labels.Add(nameof(MetaDataType.Identity), loggingEvent.Identity ?? unknown); } if (_withMetaData.Contains(MetaDataType.ThreadName)) { labels.Add(nameof(MetaDataType.ThreadName), loggingEvent.ThreadName ?? unknown); } if (_withMetaData.Contains(MetaDataType.UserName)) { labels.Add(nameof(MetaDataType.UserName), loggingEvent.UserName ?? unknown); } if (_withMetaData.Contains(MetaDataType.Domain)) { labels.Add(nameof(MetaDataType.Domain), loggingEvent.Domain ?? unknown); } if (_withMetaData.Contains(MetaDataType.LoggerName)) { labels.Add(nameof(MetaDataType.LoggerName), loggingEvent.LoggerName ?? unknown); } if (_withMetaData.Contains(MetaDataType.Level)) { labels.Add(nameof(MetaDataType.Level), loggingEvent.Level?.Name ?? unknown); } TryAddGitRevisionId(labels); foreach (var customLabel in _customLabels) { labels.Add(customLabel.Key, customLabel.Value); } var logEntry = new LogEntry { TextPayload = RenderLoggingEvent(loggingEvent), Severity = s_levelMap[loggingEvent.Level], Timestamp = loggingEvent.TimeStamp.ToTimestamp(), LogName = _logName, Resource = _resource, Labels = { labels }, }; if (sourceLocation != null) { logEntry.SourceLocation = sourceLocation; } return(logEntry); }
public static string WriteLog( string msg, [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0, [CallerMemberName] string memberName = "") { var client = LoggingServiceV2Client.Create(); var resource = new MonitoredResource { Type = "global" }; resource.Labels["project_id"] = ProjectId; LogName logName = new LogName(ProjectId, LogId); LogEntrySourceLocation sourceLocation = new LogEntrySourceLocation { File = sourceFilePath, Line = sourceLineNumber, Function = memberName }; LogEntry logEntry = new LogEntry { LogNameAsLogName = logName, Severity = LogSeverity.Info, SourceLocation = sourceLocation, TextPayload = msg }; IDictionary <string, string> entryLabels = new Dictionary <string, string> { { "size", "large" }, { "color", "red" } }; TryAddGitRevisionId(entryLabels); client.WriteLogEntries( logName: logName, resource: resource, labels: entryLabels, entries: new[] { logEntry }, callSettings: null); return(logEntry.ToString()); }
private LogEntrySourceLocation BuildLogEntryLocation(LoggingEvent loggingEvent) { string file = null; long? line = null; string function = null; if (loggingEvent.LocationInformation?.FileName != null) { file = loggingEvent.LocationInformation?.FileName; } long lineNumber; if (long.TryParse(loggingEvent.LocationInformation?.LineNumber, out lineNumber)) { line = lineNumber; } string function0 = null; string fullTypeName = loggingEvent.LocationInformation?.ClassName; if (fullTypeName != null) { var type = _typeCache.GetOrAdd(fullTypeName, () => { try { return(AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => a.GetTypes()) .FirstOrDefault(t => t.FullName == fullTypeName)); } catch { // Ignore exceptions. This is best-effort only, and expected to fail in some situations. // Returning null caches the failure. return(null); } }); if (type != null) { function0 = type.AssemblyQualifiedName; } } string function1 = loggingEvent.LocationInformation?.MethodName; if (function0 != null || function1 != null) { function = $"[{function0 ?? ""}].{function1 ?? ""}"; } if (file != null || line != null || function != null) { var sourceLocation = new LogEntrySourceLocation(); if (file != null) { sourceLocation.File = file; } if (line != null) { sourceLocation.Line = line.Value; } if (function != null) { // Format of "function" is: // "[<assembly-qualified type name>].<method name>" // E.g. "[Google.Cloud.Logging.Log4Net.Tests.Log4NetTest, Google.Cloud.Logging.Log4Net.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=185c282632e132a0].LogInfo" sourceLocation.Function = function; } return(sourceLocation); } return(null); }
private LogEntry BuildLogEntry(LoggingEvent loggingEvent) { const string unknown = "[unknown]"; var labels = new Dictionary <string, string>(); LogEntrySourceLocation sourceLocation = null; if (_withMetaData.Contains(MetaDataType.Location)) { sourceLocation = BuildLogEntryLocation(loggingEvent); } if (_withMetaData.Contains(MetaDataType.Identity)) { labels.Add(nameof(MetaDataType.Identity), loggingEvent.Identity ?? unknown); } if (_withMetaData.Contains(MetaDataType.ThreadName)) { labels.Add(nameof(MetaDataType.ThreadName), loggingEvent.ThreadName ?? unknown); } if (_withMetaData.Contains(MetaDataType.UserName)) { labels.Add(nameof(MetaDataType.UserName), loggingEvent.UserName ?? unknown); } if (_withMetaData.Contains(MetaDataType.Domain)) { labels.Add(nameof(MetaDataType.Domain), loggingEvent.Domain ?? unknown); } if (_withMetaData.Contains(MetaDataType.LoggerName)) { labels.Add(nameof(MetaDataType.LoggerName), loggingEvent.LoggerName ?? unknown); } if (_withMetaData.Contains(MetaDataType.Level)) { labels.Add(nameof(MetaDataType.Level), loggingEvent.Level?.Name ?? unknown); } TryAddGitRevisionId(labels); if (_customLabelsPatterns != null) { foreach (var pattern in _customLabelsPatterns) { labels.Add(pattern.Key, pattern.Layout.Format(loggingEvent)); } } else { foreach (var customLabel in _customLabels) { labels.Add(customLabel.Key, customLabel.Value); } } var logEntry = new LogEntry { Severity = s_levelMap[loggingEvent.Level], Timestamp = loggingEvent.TimeStamp.ToTimestamp(), LogName = _logName, Resource = _resource, Labels = { labels }, }; // Note that we can't just unconditionally set both TextPayload and JsonPayload, as they're items in a oneof in the proto. var jsonPayload = JsonLayout?.Format(loggingEvent); if (jsonPayload is null) { logEntry.TextPayload = RenderLoggingEvent(loggingEvent); } else { logEntry.JsonPayload = jsonPayload; } if (sourceLocation != null) { logEntry.SourceLocation = sourceLocation; } return(logEntry); }