示例#1
0
 public GeneratePdf(IRazorViewToStringRenderer engine)
 {
     _engine         = engine;
     _convertOptions = new ConvertOptions();
 }
示例#2
0
 public void SetConvertOptions(IConvertOptions convertOptions)
 {
     _convertOptions = convertOptions;
 }
示例#3
0
        /// <inheritdoc />
        public JObject GetGelfJson(LogEventInfo logEventInfo, IConvertOptions options)
        {
            if (logEventInfo == null)
            {
                throw new ArgumentNullException(nameof(logEventInfo));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            // Retrieve the formatted message from LogEventInfo
            var logEventMessage = logEventInfo.FormattedMessage;
            var properties      = new Dictionary <object, object>(logEventInfo.Properties);

            if (options.IncludeMdlcProperties)
            {
                foreach (var propertyName in MappedDiagnosticsLogicalContext.GetNames())
                {
                    properties[propertyName] = MappedDiagnosticsLogicalContext.GetObject(propertyName);
                }
            }

            if (logEventMessage == null)
            {
                return(null);
            }

            // If we are dealing with an exception, pass exception properties to LogEventInfo properties
            var exception = logEventInfo.Exception;

            if (exception != null)
            {
                var exceptionLevel = 0;

                do
                {
                    var prefix = $"Exception.{exceptionLevel++}.";
                    properties[prefix + "Type"]       = exception.GetType().FullName;
                    properties[prefix + "Source"]     = exception.Source;
                    properties[prefix + "Message"]    = exception.Message;
                    properties[prefix + "StackTrace"] = exception.StackTrace;

                    exception = exception.InnerException;
                }while (exception != null);
            }

            // Figure out the short message
            var shortMessage = logEventMessage;

            if (shortMessage.Length > ShortMessageMaxLength)
            {
                shortMessage = shortMessage.Substring(0, ShortMessageMaxLength);
            }

            // Construct the instance of GelfMessage
            // See http://docs.graylog.org/en/2.4/pages/gelf.html "Specification (version 1.1)"
            var gelfMessage = new GelfMessage
            {
                Version      = GelfVersion,
                Host         = Dns.GetHostName(),
                ShortMessage = shortMessage,
                FullMessage  = logEventMessage,
                Timestamp    = logEventInfo.TimeStamp.ToUnixTimestamp(),
                Level        = GetSeverityLevel(logEventInfo.Level),
            };

            // Convert to JSON
            var jsonObject = JObject.FromObject(gelfMessage);

            // Add any other interesting data to LogEventInfo properties
            properties["LoggerName"] = logEventInfo.LoggerName;
            properties["facility"]   = options.Facility;

            // We will persist them "Additional Fields" according to Gelf spec
            foreach (var property in properties)
            {
                AddAdditionalField(jsonObject, property, options.SerializeObjectProperties);
            }

            return(jsonObject);
        }