Esempio n. 1
0
        // syslog 형식에 맞춰서 메시지를 만든 후 udp로 asyncronous 하게 전송
        public static async void portableSyslogUdpSend(String host, int port, String msg)
        {
            var process = Process.GetCurrentProcess();
            SyslogRfc5424MessageSerializer syslogRfc5424MessageSerializerUtf8 = new SyslogRfc5424MessageSerializer(Encoding.UTF8);

            // making syslog(rfc5424, rfc3164) message format
            var message = new SyslogMessage(DateTime.Now,
                                            facility: Facility.UserLevelMessages,
                                            severity: Severity.Debug,
                                            hostName: process.MachineName,
                                            procId: process.Id.ToString(),
                                            appName: process.ProcessName,
                                            msgId: "hyundai motors security log",
                                            message: msg);

            try
            {
                // asynchronous communication because of performance
                using (var sender = new AsyncSyslogUdpSender(host, port))
                {
                    await sender.ConnectAsync();

                    await sender.SendAsync(message, syslogRfc5424MessageSerializerUtf8);

                    await sender.DisconnectAsync();
                }
            }
            catch (Exception e)
            {
                _localLog.WriteEntry("upd send error : " + e.ToString(), EventLogEntryType.Error);
            }
        }
Esempio n. 2
0
        private static void UsingSyslog()
        {
            var payload = new LogEntity
            {
                IntField    = 7,
                LongField   = 1200400,
                StringField = "Connection error with endpoint",
            };

            ISyslogMessageSender     sender     = new SyslogUdpSender(syslogAddress, syslogPort);
            ISyslogMessageSerializer serializer = new SyslogRfc5424MessageSerializer();

            var msg = new SyslogMessage(
                DateTimeOffset.Now,                  //DatetimeOffset
                Facility.UserLevelMessages,          //Facility
                Severity.Debug,                      //Severity
                Environment.MachineName,             //HostName
                "Open",                              //Appname
                "0",                                 //ProcId
                "0",                                 //MsgId
                JsonConvert.SerializeObject(payload) //Message
                );

            for (int counter = 0; counter < 1; counter++)
            {
                sender.Send(msg, new SyslogRfc5424MessageSerializer());
            }
        }
        public static string Serialize(this SyslogRfc5424MessageSerializer serializer, SyslogMessage message)
        {
            using (var stream = new MemoryStream())
            {
                serializer.Serialize(message, stream);
                stream.Flush();
                stream.Position = 0;

                using (var reader = new StreamReader(stream, Encoding.UTF8))
                    return(reader.ReadLine());
            }
        }
        private const string AppName      = "My Event Log";         // <<<!!! INSERT YOUR SYSLOG APP-NAME HERE !!!>>>

        /// <summary>
        /// C# applications have an entry point called the Main Method.
        /// It is the first method that gets invoked when an application starts.
        /// </summary>
        /// <param name="args">Command line arguments as string type parameters</param>
        static void Main(string[] args)
        {
            try
            {
                // Create a new SyslogUdpSender with the SyslogServer and SyslogPort provided.
                // The using block ensures the SyslogUdpSender is automatically closed.
                using (SyslogUdpSender syslogSender = new SyslogUdpSender(SyslogServer, SyslogPort))
                {
                    // Set the message to send.
                    string messageToSend = "Test message at " + DateTime.Now;

                    /////////////////////////////////////////////
                    // Send an RFC 3164 Syslog Message. See:
                    // https://tools.ietf.org/html/rfc3164
                    /////////////////////////////////////////////
                    // In RFC 3164, the message component (known as MSG) was specified as having these fields: TAG,
                    // which should be the name of the program or process that generated the message, and CONTENT which
                    // contains the details of the message.
                    // The content field should be encoded in a UTF-8 character set and octet values in the traditional
                    // ASCII control character range should be avoided.
                    /////////////////////////////////////////////

                    // Display progress.
                    Console.WriteLine("Sending RFC3164 Syslog message: " + messageToSend);

                    // Create a new Syslog message (RFC3164).
                    var rfc3164SyslogMessage = new SyslogMessage(DateTimeOffset.Now,                    // TIMESTAMP
                                                                 Facility.UserLevelMessages,            // FACILITY
                                                                 Severity.Informational,                // SEVERITY
                                                                 Environment.MachineName,               // HOSTNAME
                                                                 AppName,                               // APP-NAME
                                                                 messageToSend);                        // MSG

                    // Create a new Syslog RFC3164 serializer.
                    var rfc3164SyslogSerializer = new SyslogRfc3164MessageSerializer();

                    // Display the serialised message to be sent.
                    Console.Write(Environment.NewLine);
                    Console.WriteLine(rfc3164SyslogSerializer.Serialize(rfc3164SyslogMessage));
                    Console.Write(Environment.NewLine);

                    // Send the Syslog message, using the serializer created.
                    syslogSender.Send(rfc3164SyslogMessage, rfc3164SyslogSerializer);

                    // Display a confirmation.
                    Console.WriteLine("RFC3164 message was successfully sent to Syslog Server :-)");

                    /////////////////////////////////////////////
                    // Send an RFC 5424 Syslog Message. See:
                    // https://tools.ietf.org/html/rfc5424
                    /////////////////////////////////////////////
                    // In RFC 3164, the message component (known as MSG) was specified as having these fields: TAG,
                    // which should be the name of the program or process that generated the message, and CONTENT which
                    // contains the details of the message.
                    // Described in RFC 5424,[9] "MSG is what was called CONTENT in RFC 3164. The TAG is now part of the
                    // header, but not as a single field. The TAG has been split into APP-NAME, PROCID, and MSGID.
                    // This does not totally resemble the usage of TAG, but provides the same functionality for most of
                    // the cases." Popular syslog tools such as Rsyslog conform to this new standard.
                    // The content field should be encoded in a UTF-8 character set and octet values in the traditional
                    // ASCII control character range should be avoided.
                    /////////////////////////////////////////////

                    // Display progress.
                    Console.Write(Environment.NewLine);
                    Console.WriteLine("Sending RFC5424 Syslog message: " + messageToSend);

                    // Create structured data items for RFC5424 compliance.
                    var sdi1   = "myexampleSDID@12345";
                    var key1   = "myeventId";
                    var value1 = "1234";

                    var sdi2   = "myexampleSDID@23456";
                    var key2   = "myeventSource";
                    var value2 = "My Application";

                    // Create a new StructuredDataElement list.
                    var structuredDataElements = new List <StructuredDataElement>
                    {
                        new StructuredDataElement(sdi1, new Dictionary <string, string> {
                            { key1, value1 }
                        }),
                        new StructuredDataElement(sdi2, new Dictionary <string, string> {
                            { key2, value2 }
                        }),
                    };

                    // Set the PROCID and MSGID.
                    string procId = "My ProcId";
                    string msgId  = "My MsgId";

                    // Create a new Syslog message (RFC5424).
                    SyslogMessage rfc5424SyslogMessage = new SyslogMessage(DateTimeOffset.Now,                // TIMESTAMP
                                                                           Facility.UserLevelMessages,        // FACILITY
                                                                           Severity.Informational,            // SEVERITY
                                                                           Environment.MachineName,           // HOSTNAME
                                                                           AppName,                           // APP-NAME
                                                                           procId,                            // PROCID
                                                                           msgId,                             // MSGID
                                                                           messageToSend,                     // MSG
                                                                           structuredDataElements.ToArray()); // STRUCTURED-DATA

                    // Create a new Syslog RFC5424 serializer.
                    var rfc5424SyslogSerializer = new SyslogRfc5424MessageSerializer();

                    // Display the serialised message to be sent.
                    Console.Write(Environment.NewLine);
                    Console.WriteLine(rfc5424SyslogSerializer.Serialize(rfc5424SyslogMessage));
                    Console.Write(Environment.NewLine);

                    // Send the Syslog message, using the serializer created.
                    syslogSender.Send(rfc5424SyslogMessage, rfc5424SyslogSerializer);

                    // Display a confirmation.
                    Console.WriteLine("RFC5424 message was successfully sent to Syslog Server :-)");

                    Console.ReadKey(true);
                }
            }
            catch (Exception ex)
            {
                // Display an error.
                Console.WriteLine("Failed to send the message to Syslog :-(");
                Console.Write(Environment.NewLine);
                Console.WriteLine(ex.ToString());

                Console.ReadKey(true);
            }
        }
 public SyslogRfc5424MessageSerializerTests()
 {
     sut = new SyslogRfc5424MessageSerializer();
 }