Beispiel #1
0
        public static void WriteLine(string message, string level)
        {
            // create a json record
            var record = new TraceRecord()
            {
                Deployment = HostEnvironment.DeploymentName,
                Role = HostEnvironment.AzureRoleName,
                LogLevel = level,
                Session = TraceLog.Session,
                Message = message,
                Timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff")
            };
            var json = JsonSerializer.Serialize(record) + "\n";
            byte[] buffer = UTF8Encoding.UTF8.GetBytes(json);

            lock (writeLock)
            {
                // enter a retry loop for sending the buffer on the splunk socket
                int retryCount = 2;
                while (retryCount > 0)
                {
                    try
                    {
                        TraceSocket.Send(buffer);
                        // success - terminate the enclosing retry loop
                        break;
                    }
                    catch (Exception ex)
                    {
                        TraceFile.WriteLine(ex.Message, "Error");
                        TraceFile.WriteLine(ex.StackTrace, "Error");

                        // the socket wasn't opened or written to correctly - try to start with a new socket in the next iteration of the retry loop
                        if (traceSocket != null)
                            traceSocket.Dispose();
                        traceSocket = null;
                        retryCount--;
                    }
                }

                // multiple socket failures - log to a file as a last resort
                if (retryCount == 0)
                    TraceFile.WriteLine(message, level);
            }
        }
Beispiel #2
0
        public static void WriteLine(string message, string level)
        {
            // create a json record
            var record = new TraceRecord()
            {
                Deployment = HostEnvironment.DeploymentName,
                Role       = HostEnvironment.AzureRoleName,
                LogLevel   = level,
                Message    = message,
                Session    = TraceLog.Session,
                Timestamp  = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff")
            };
            var json = JsonSerializer.Serialize(record);

            lock (writeLock)
            {
                // enter a retry loop writing the record to the trace file
                int retryCount = 2;
                while (retryCount > 0)
                {
                    try
                    {
                        if (traceFilename == null)
                        {
                            // create the file
                            using (var stream = File.Create(TraceFilename))
                                using (var writer = new StreamWriter(stream))
                                {
                                    // log the file creation
                                    var createRecord = new TraceRecord()
                                    {
                                        Deployment = HostEnvironment.DeploymentName,
                                        Role       = HostEnvironment.AzureRoleName,
                                        LogLevel   = TraceLog.LevelText(TraceLog.LogLevel.Info),
                                        Message    = "Created new trace file " + TraceFilename,
                                        Session    = TraceLog.Session,
                                        Timestamp  = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff")
                                    };
                                    var createJson = JsonSerializer.Serialize(createRecord);
                                    writer.WriteLine(createJson);
                                    writer.Flush();
                                }
                        }

                        // open the file
                        using (var stream = File.Open(TraceFilename, FileMode.Append, FileAccess.Write, FileShare.Read))
                            using (var writer = new StreamWriter(stream))
                            {
                                writer.WriteLine(json);
                                writer.Flush();

                                // reset the trace filename if it exceeds the maximum file size
                                if (writer.BaseStream.Position > MaxFileSize)
                                {
                                    traceFilename = null;
                                }
                            }

                        // success - terminate the enclosing retry loop
                        break;
                    }
                    catch (Exception)
                    {
                        // the file wasn't opened or written to correctly - try to start with a new file in the next iteration of the retry loop
                        traceFilename = null;
                        retryCount--;
                    }
                }
            }
        }
Beispiel #3
0
        public static void WriteLine(string message, string level)
        {
            // create a json record
            var record = new TraceRecord()
            {
                Deployment = HostEnvironment.DeploymentName,
                Role = HostEnvironment.AzureRoleName,
                LogLevel = level,
                Message = message,
                Session = TraceLog.Session,
                Timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff")
            };
            var json = JsonSerializer.Serialize(record);

            lock (writeLock)
            {
                // enter a retry loop writing the record to the trace file
                int retryCount = 2;
                while (retryCount > 0)
                {
                    try
                    {
                        if (traceFilename == null)
                        {
                            // create the file
                            using (var stream = File.Create(TraceFilename))
                            using (var writer = new StreamWriter(stream))
                            {
                                // log the file creation
                                var createRecord = new TraceRecord()
                                {
                                    Deployment = HostEnvironment.DeploymentName,
                                    Role = HostEnvironment.AzureRoleName,
                                    LogLevel = TraceLog.LevelText(TraceLog.LogLevel.Info),
                                    Message = "Created new trace file " + TraceFilename,
                                    Session = TraceLog.Session,
                                    Timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff")
                                };
                                var createJson = JsonSerializer.Serialize(createRecord);
                                writer.WriteLine(createJson);
                                writer.Flush();
                            }
                        }

                        // open the file
                        using (var stream = File.Open(TraceFilename, FileMode.Append, FileAccess.Write, FileShare.Read))
                        using (var writer = new StreamWriter(stream))
                        {
                            writer.WriteLine(json);
                            writer.Flush();

                            // reset the trace filename if it exceeds the maximum file size
                            if (writer.BaseStream.Position > MaxFileSize)
                                traceFilename = null;
                        }

                        // success - terminate the enclosing retry loop
                        break;
                    }
                    catch (Exception)
                    {
                        // the file wasn't opened or written to correctly - try to start with a new file in the next iteration of the retry loop
                        traceFilename = null;
                        retryCount--;
                    }
                }
            }
        }