Last active
September 13, 2022 07:29
-
-
Save z3niths/9ec94e2186d2ba1e94d47f21ba96dcd1 to your computer and use it in GitHub Desktop.
Logging client libraries
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
Install : | |
`dotnet add package Google.Cloud.Logging.V2` |
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
private void DeleteLog(string logId) | |
{ | |
var client = LoggingServiceV2Client.Create(); | |
LogName logName = new LogName(s_projectId, logId); | |
client.DeleteLog(logName, _retryAWhile); | |
Console.WriteLine($"Deleted {logId}."); | |
} |
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
private void ListLogEntries(string logId) | |
{ | |
var client = LoggingServiceV2Client.Create(); | |
LogName logName = new LogName(s_projectId, logId); | |
ProjectName projectName = new ProjectName(s_projectId); | |
var results = client.ListLogEntries(Enumerable.Repeat(projectName, 1), $"logName={logName.ToString()}", | |
"timestamp desc", callSettings: _retryAWhile); | |
foreach (var row in results) | |
{ | |
Console.WriteLine($"{row.TextPayload.Trim()}"); | |
} | |
} |
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
using System; | |
// Imports the Google Cloud Logging client library | |
using Google.Cloud.Logging.V2; | |
using Google.Cloud.Logging.Type; | |
using System.Collections.Generic; | |
using Google.Api; | |
namespace GoogleCloudSamples | |
{ | |
public class QuickStart | |
{ | |
public static void Main(string[] args) | |
{ | |
// Your Google Cloud Platform project ID. | |
string projectId = "YOUR-PROJECT-ID"; | |
// Instantiates a client. | |
var client = LoggingServiceV2Client.Create(); | |
// Prepare new log entry. | |
LogEntry logEntry = new LogEntry(); | |
string logId = "my-log"; | |
LogName logName = new LogName(projectId, logId); | |
logEntry.LogNameAsLogName = logName; | |
logEntry.Severity = LogSeverity.Info; | |
// Create log entry message. | |
string message = "Hello World!"; | |
string messageId = DateTime.Now.Millisecond.ToString(); | |
Type myType = typeof(QuickStart); | |
string entrySeverity = logEntry.Severity.ToString().ToUpper(); | |
logEntry.TextPayload = | |
$"{messageId} {entrySeverity} {myType.Namespace}.LoggingSample - {message}"; | |
// Set the resource type to control which GCP resource the log entry belongs to. | |
// See the list of resource types at: | |
// https://cloud.google.com/logging/docs/api/v2/resource-list | |
// This sample uses resource type 'global' causing log entries to appear in the | |
// "Global" resource list of the Developers Console Logs Viewer: | |
// https://console.cloud.google.com/logs/viewer | |
MonitoredResource resource = new MonitoredResource | |
{ | |
Type = "global" | |
}; | |
// Create dictionary object to add custom labels to the log entry. | |
IDictionary<string, string> entryLabels = new Dictionary<string, string>(); | |
entryLabels.Add("size", "large"); | |
entryLabels.Add("color", "red"); | |
// Add log entry to collection for writing. Multiple log entries can be added. | |
IEnumerable<LogEntry> logEntries = new LogEntry[] { logEntry }; | |
// Write new log entry. | |
client.WriteLogEntries(logName, resource, entryLabels, logEntries); | |
Console.WriteLine("Log Entry created."); | |
} | |
} | |
} |
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
private void WriteLogEntry(string logId, string message) | |
{ | |
var client = LoggingServiceV2Client.Create(); | |
LogName logName = new LogName(s_projectId, logId); | |
LogEntry logEntry = new LogEntry | |
{ | |
LogNameAsLogName = logName, | |
Severity = LogSeverity.Info, | |
TextPayload = $"{typeof(LoggingSample).FullName} - {message}" | |
}; | |
MonitoredResource resource = new MonitoredResource { Type = "global" }; | |
IDictionary<string, string> entryLabels = new Dictionary<string, string> | |
{ | |
{ "size", "large" }, | |
{ "color", "red" } | |
}; | |
client.WriteLogEntries(logName, resource, entryLabels, | |
new[] { logEntry }, _retryAWhile); | |
Console.WriteLine($"Created log entry in log-id: {logId}."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment