Jamie Dixon
Jamie Dixon

Reputation: 4292

LogEntries without a .config file in a .NET application

I am reading the article on logging from .NET Applications found here. I would like to do the functional equivalent without using a .config file. Does anyone have code samples for that? Thanks

Upvotes: 0

Views: 399

Answers (2)

Rodolpho Brock
Rodolpho Brock

Reputation: 8155

I think this code is wrong... could you check? By the way, I made work this way:

    target.Token = "LOG_TOKEN";
    target.Ssl = false;
    target.Debug = true;
    target.Name = "Logentries";
    target.Layout = "${date:format=ddd MMM dd} ${time:format=HH:mm:ss} ${date:format=zzz yyyy} ${logger} : ${LEVEL}, ${message}";
    target.HttpPut = false;

    var config = new LoggingConfiguration();
    config.AddTarget("Logentries", target);

    var loggingRule = new LoggingRule("*", LogLevel.Debug, target);
    config.LoggingRules.Add(loggingRule);

    LogManager.Configuration = config;
    LogManager.Configuration.Reload();

Upvotes: 1

Stephen Hynes
Stephen Hynes

Reputation: 31

Sure we can do do this. For nlog we can do the following.

        class Program
        {
            public static LogentriesTarget target = new LogentriesTarget();
            private static Logger logger = LogManager.GetCurrentClassLogger();
            static void Main(string[] args)
            {
                var config = new LoggingConfiguration();
                target.Token = "LOG_TOKEN";
                target.Ssl = false;
                target.Debug = true;
                target.Name = "Logentries";
                target.Layout = "${date:format=ddd MMM dd} ${time:format=HH:mm:ss} ${date:format=zzz yyyy} ${logger} : ${LEVEL}, ${message}";
                target.HttpPut = false;
                config.AddTarget("Logentries2", target);
                var loggingRule = new LoggingRule("*", LogLevel.Debug, target);
                LogManager.Configuration.AddTarget("targetName", target);
                LogManager.Configuration.LoggingRules.Add(loggingRule);
                LogManager.Configuration.Reload();
                logger = LogManager.GetCurrentClassLogger();
            }
        }

For log4net we can do the following.

        class Program
        {
            private static readonly ILog logger = LogManager.GetLogger(typeof(Program));
            static void Main(string[] args)
            {
                LogentriesAppender appender = new LogentriesAppender();
                appender.Token = "YOUR_TOKEN";
                appender.Name = "LogentriesAppender";
                PatternLayout layout = new PatternLayout();
                layout.ConversionPattern = "%d{ddd MMM dd HH:mm:ss zzz yyyy} %logger %: %level%, %m,";
                layout.ActivateOptions();
                appender.Layout = layout;
                Logger l = (Logger)logger.Logger;
                l.AddAppender(appender);
                logger.Debug("Hi");
            }
        }

Finally the other alternative is using Serilog for sending your log data to us.

Regards, Stephen

Upvotes: 3

Related Questions