Logging for C# SDK

How to enable logging

Required UserId

Always set the UserId to uniquely identify the user or device that connects to PubNub. This UserId should be persisted, and should remain unchanged for the lifetime of the user or the device. If you don't set the UserId, you won't be able to connect to PubNub.


var pubnubConfiguration = new PNConfiguration(new UserId("uniqueUserId"))
{
SubscribeKey = "[yourSubscribeKey]",
PublishKey = "[yourPublishKey]",
LogLevel = PubnubLogLevel.Debug,
};
var pubnub = new Pubnub(pubnubConfiguration);

var customLogger = new PubnubConsoleLogger();
pubnub.SetLogger(customLogger);

// A custom logger that logs information on console.
// Use can implement logger that can log information using log4Net or file etc.
public class PubnubConsoleLogger : IPubnubLogger
show all 36 lines

Additional logging details

  • LogLevel options: The SDK supports various log levels: PubnubLogLevel.All, PubnubLogLevel.Trace, PubnubLogLevel.Debug, PubnubLogLevel.Info, PubnubLogLevel.Warn, PubnubLogLevel.Error, and PubnubLogLevel.None (which disables logging). Only logs with severity equal to or higher than the configured level will be generated.

  • Default logging: The SDK automatically initializes a basic logger when you create a Pubnub instance. The log level you specify in your configuration controls what level of logs are generated.

  • Multiple loggers: You can attach multiple custom loggers by calling SetLogger() with different logger implementations. This allows you to simultaneously log to different outputs (console, file, etc.).

Last updated on