Reputation: 1985
I want to use Logz.io in .Net
as a singleton service. The current documentation is not covering this for now.
This is some code I made until now while trying to understand how can I achive my goal...
I added ILogzioService
as a dependency to one of my REST API endpoints and tried to push some data, but I can't see any log in Kibana and I don't get any error neither...
public class LogzioService : ILogzioService
{
private readonly string APPNAME = "MyApp0";
public ILogger logger;
private ILoggerRepository loggerRepository;
private Hierarchy hierarchy;
//private LogzioAppender logzioAppender;
public LogzioService(IConfiguration config)
{
hierarchy = (Hierarchy)LogManager.GetRepository();
LogzioAppender logzioAppender = new LogzioAppender();
logzioAppender.AddToken(config["logzio_key"]);
logzioAppender.AddType("log4net");
logzioAppender.AddListenerUrl("listener-nl.logz.io:8071"); // Azure - West Europe
logzioAppender.AddBufferSize(100);
logzioAppender.AddBufferTimeout(TimeSpan.FromSeconds(5));
logzioAppender.AddRetriesMaxAttempts(3);
logzioAppender.AddRetriesInterval(TimeSpan.FromSeconds(2));
logzioAppender.AddDebug(false);
logzioAppender.AddGzip(true);
logzioAppender.JsonKeysCamelCase(false);
// <-- Uncomment and edit this line to enable proxy routing: -->
// logzioAppender.AddProxyAddress("http://your.proxy.com:port");
// <-- Uncomment this to prase messages as Json -->
logzioAppender.ParseJsonMessage(true);
hierarchy.Root.AddAppender(logzioAppender);
hierarchy.Configured = true;
LogzioAppenderCustomField sourceField = new LogzioAppenderCustomField();
sourceField.Key = "source";
sourceField.Value = APPNAME;
logzioAppender.AddCustomField(sourceField);
logger = hierarchy.GetLogger(APPNAME);
}
public void LogCritical(string message, Exception ex)
{
LoggingEventData loggingEventData = new LoggingEventData();
loggingEventData.Message = message;
loggingEventData.Level = Level.Critical;
loggingEventData.Domain = "Domain";
loggingEventData.ThreadName = "ThreadName";
loggingEventData.ExceptionString = ex.ToString() + "\r\n" + ex.StackTrace;
loggingEventData.Identity = "Identity";
loggingEventData.TimeStampUtc = DateTime.UtcNow;
loggingEventData.LoggerName = "LoggerName";
loggingEventData.UserName = "UserName";
loggingEventData.LocationInfo = new LocationInfo(GetType()); // not sure how to use this
//loggingEventData.Properties = new log4net.Util.PropertiesDictionary(); // not sure how to use this
// method 1
LoggingEvent loggingEvent = new LoggingEvent(GetType(), loggerRepository, loggingEventData);
// method 2
//LoggingEvent loggingEvent = new LoggingEvent(GetType(), loggerRepository, loggingEventData.LoggerName, loggingEventData.Level, loggingEventData.Message, ex);
hierarchy.Log(loggingEvent); // not working
logger.Log(loggingEvent); // not working
}
}
Upvotes: 0
Views: 308
Reputation: 26
I looked into your code and managed to make it work.
Basically I added logzioAppender.ActiveOptions();
and added https://
to the logzioAppender.AddListenerUrl
.
I highly suggest that you set logzioAppender.AddDebug(true)
it will show you all the debug logs of the Logz.io appender. Also it takes couple of seconds for the logs to be shipped to Logz.io.
Hope it will work for you :)
public class LogzioService : ILogzioService
{
private readonly string APPNAME = "MyApp0";
public ILogger logger;
private ILoggerRepository loggerRepository;
private Hierarchy hierarchy;
public LogzioService(IConfiguration config)
{
hierarchy = (Hierarchy)LogManager.GetRepository();
LogzioAppender logzioAppender = new LogzioAppender();
logzioAppender.AddToken(config["logzio_key"]);
logzioAppender.AddType("log4net");
logzioAppender.AddListenerUrl("https://listener-nl.logz.io:8071"); // Azure - West Europe
logzioAppender.AddBufferSize(100);
logzioAppender.AddBufferTimeout(TimeSpan.FromSeconds(5));
logzioAppender.AddRetriesMaxAttempts(3);
logzioAppender.AddRetriesInterval(TimeSpan.FromSeconds(2));
logzioAppender.AddDebug(false);
logzioAppender.AddGzip(true);
logzioAppender.JsonKeysCamelCase(false);
// <-- Uncomment and edit this line to enable proxy routing: -->
// logzioAppender.AddProxyAddress("http://your.proxy.com:port");
// <-- Uncomment this to prase messages as Json -->
logzioAppender.ParseJsonMessage(true);
LogzioAppenderCustomField sourceField = new LogzioAppenderCustomField();
sourceField.Key = "source";
sourceField.Value = APPNAME;
logzioAppender.AddCustomField(sourceField);
logzioAppender.ActiveOptions();
hierarchy.Root.AddAppender(logzioAppender);
hierarchy.Root.Level = Level.All;
hierarchy.Configured = true;
logger = hierarchy.GetLogger(APPNAME);
}
public void LogCritical(string message, Exception ex)
{
LoggingEventData loggingEventData = new LoggingEventData();
loggingEventData.Message = message;
loggingEventData.Level = Level.Critical;
loggingEventData.Domain = "Domain";
loggingEventData.ThreadName = "ThreadName";
loggingEventData.ExceptionString = ex.ToString() + "\r\n" + ex.StackTrace;
loggingEventData.Identity = "Identity";
loggingEventData.TimeStampUtc = DateTime.UtcNow;
loggingEventData.LoggerName = "LoggerName";
loggingEventData.UserName = "UserName";
loggingEventData.LocationInfo = new LocationInfo(GetType());
LoggingEvent loggingEvent = new LoggingEvent(GetType(), loggerRepository, loggingEventData);
hierarchy.Log(loggingEvent);
logger.Log(loggingEvent);
}
}
Upvotes: 1