Пример #1
0
        private static SyslogNet.Client.SyslogMessage CreateSyslogMessage(
            SyslogNet.Client.SyslogOptions options
            , string message)
        {
            // https://www.syslog-ng.com/technical-documents/doc/syslog-ng-open-source-edition/3.16/administration-guide/option-description-log-msg-size
            // Description: Maximum length of a message in bytes.
            // This length includes the entire message
            // (the data structure and individual fields).
            // The maximal value that can be set is 268435456 bytes(256MB).
            // For messages using the IETF-syslog message format(RFC5424),
            // the maximal size of the value of an SDATA field is 64kB.
            // In most cases, it is not recommended to set log-msg-size()
            // higher than 10 MiB.

            // https://stackoverflow.com/questions/3310875/find-syslog-max-message-length
            // Keep in mind syslog is a protocol,
            // which means it sets minimums and makes recommendations.
            // I can't find a source to this, but I believe
            // the minimum length that should be supported is 1k,
            // with 64k being recommended.


            System.Collections.Generic.Dictionary <string, string> sd = new System.Collections.Generic.Dictionary <string, string>(System.StringComparer.InvariantCultureIgnoreCase);
            sd["Hello"]    = "World";
            sd["Привет"]   = "мир";
            sd["你好"]       = "世界";
            sd["nixda"]    = "[]";
            sd["foo[]bar"] = "{}";


            SyslogNet.Client.StructuredDataElement sde = new SyslogNet.Client.StructuredDataElement("sdld", sd);


            // Each implementation is free to do what they want,
            // i.e. if you wanted a 16MB maximum and were writing
            // a syslog server, you're free to do that.
            // I'm not sure why you would, but you could.
            // As far as I know, there is no standard programatic way
            // of ascertaining this, so keeping messages at
            // just under 1k would be ideal for portability.
            return(new SyslogNet.Client.SyslogMessage(
                       System.DateTimeOffset.Now,
                       SyslogNet.Client.Facility.UserLevelMessages,
                       SyslogNet.Client.Severity.Error,
                       options.LocalHostName,
                       options.AppName,
                       options.ProcId,
                       options.MsgType,
                       message ?? (options.Message ??
                                   "Test message at "
                                   + System.DateTime.UtcNow.ToString("dddd, dd.MM.yyyy HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture))
                       , sde
                       ));
        }         // End Function CreateSyslogMessage
Пример #2
0
        private SyslogNet.Client.SyslogMessage BuildSyslogMessage(LogEvent logEvent)
        {
            var formattedMessage = this.GetFormattedMessage(logEvent);
            var severity         = GetSyslogSeverity(logEvent.Level);

            DateTimeOffset dtOffset = new DateTimeOffset(DateTime.Now.ToUniversalTime());
            var            facility = SyslogNet.Client.Facility.LocalUse1;
            string         procId   = System.Diagnostics.Process.GetCurrentProcess().Id.ToString();
            string         msgId    = null;
            string         hostName = Dns.GetHostName();

            Dictionary <string, string> properties = new Dictionary <string, string>();

            foreach (var property in logEvent.Properties)
            {
                properties.Add(property.Key, property.Value.ToString());
            }
            SyslogNet.Client.StructuredDataElement sdElement = new SyslogNet.Client.StructuredDataElement("1", properties);

            SyslogNet.Client.SyslogMessage message = new SyslogNet.Client.SyslogMessage(dtOffset, facility, severity, hostName, this.AppName, procId, msgId, formattedMessage, sdElement);

            return(message);
        }