示例#1
0
        /// <summary>
        /// C#-oriented-method: Write an Event to the logger. Exposes all parameters in a way that should make
        /// it very easy to use the logging behaviour from C#.
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="level"></param>
        /// <param name="template">
        /// This is the message that you want to log.
        /// It's worth noting that a message without string.Format-ed parameters, is more equal
        /// across time, and if you have custom data you want to pass, you should rather set
        /// that data on the 'data' property of Message (with SetField and SetContext*).</param>
        /// <param name="setterTransformer">
        /// There are extension methods in this library that go towards creating new instances
        /// of Message that you can use to change the value inside this function.
        /// </param>
        public static void LogEvent(
            this Logger logger,
            LogLevel level,
            string template,
            Func <Message, Message> setterTransformer)
        {
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }
            if (level == null)
            {
                throw new ArgumentNullException("level");
            }
            if (template == null)
            {
                throw new ArgumentNullException("template");
            }
            if (setterTransformer == null)
            {
                throw new ArgumentNullException("setterTransformer");
            }

            var message     = MessageModule.Event(level, template);
            var messageNext = setterTransformer(message);

            logger.Log(messageNext).Start();
        }
示例#2
0
        public static async Task SampleUsage(Logger logger)
        {
            // without async
            logger.LogSimple(MessageModule.Event(LogLevel.Info, "User logged in"));

            // await placing the Hello World event in the buffer
            await logger.LogEvent(LogLevel.Debug, "Hello world. Important? {important}", new
            {
                important = "yes"
            });

            // await logging the fatal event and getting an ack back from each of the configured
            // targets
            await logger.LogEvent(LogLevel.Fatal, "Fatal application error on finaliser thread.", waitForAck : true);

            await logger.LogEvent(LogLevel.Verbose, "We need to log this with backpressure.", new
            {
                tags = new[] { "tag1", "tag2" }
            });

            // alternatively, you can use the ack-explicit functions together with the
            // data object model that's MessageModule.
            var message = MessageModule.Event(LogLevel.Warn, "Here be dragons!");
            await logger.LogWithAck(message);

            var val = logger.Time(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    Thread.Sleep(1);
                }

                return(32);
            }, "sample.config.computeAnswerToEverything")
                          ();

            await logger.LogEventFormat(LogLevel.Warn,
                                        "{horses} is the answer to the universe and everything",
                                        val);

            await logger.Time(
                () => logger.LogEvent(LogLevel.Debug, "I wonder how long this takes"))
                ();

            try
            {
                throw new ApplicationException("thing went haywire");
            }
            catch (Exception e)
            {
                await logger.LogEventFormat(LogLevel.Fatal, "Unhandled {exception}!", e);
            }
        }
示例#3
0
        /// <summary>
        /// C#-oriented-method: Write an Event to the logger. Exposes all parameters in a way that should make
        /// it very easy to use the logging behaviour from C#.
        /// </summary>
        /// <param name="logger">Instance to invoke the extension method on</param>
        /// <param name="level"></param>
        /// <param name="template">A template message to attach to the event</param>
        /// <param name="fields">Data to attach to the log line being sent to the target;
        /// e.g. if using LogStash, these properties will be fields. For performance
        /// improvements, you can send a dictionary, or otherwise you can
        /// send an anonymous object whose public properties are then serialised
        /// as a dictionary.
        /// This is the message that you want to log.
        /// </param>
        /// <param name="timestamp">
        /// When the Message was 'actual'. Optional, defaults to when this method is called.
        /// </param>
        /// <param name="exn">
        /// Any exception associated with the event in this Message. Optional, defaults to null.
        /// </param>
        public static void LogEvent(
            this Logger logger,
            LogLevel level,
            string template,
            object fields     = null,
            Instant?timestamp = null,
            Exception exn     = null)
        {
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }
            if (level == null)
            {
                throw new ArgumentNullException("level");
            }
            if (template == null)
            {
                throw new ArgumentNullException("template");
            }

            var msg = MessageModule.Event(level, template);

            if (fields != null)
            {
                msg = msg.SetFieldsFromObject(fields);
            }
            if (timestamp != null)
            {
                msg = msg.SetTimestamp(timestamp.Value);
            }
            if (exn != null)
            {
                msg = msg.AddException(exn);
            }

            logger.Log(msg).Start();
        }