Esempio n. 1
0
        public void PublishMessage(Exception exception, string message, IDictionary <object, object> meta)
        {
            var connectionString = ConfigurationManager.ConnectionStrings["AppExceptionLog"].ConnectionString;

            if (string.IsNullOrEmpty(connectionString))
            {
                return;
            }

            if (exception is AggregateException)
            {
                exception = exception.InnerException;
            }

            var clientId         = 1;
            var formattedMessage = ExceptionFormatter.FormatText(exception, meta);
            var stackHash        = ExceptionFormatter.ComputeHash(exception.StackTrace);

            object clientIdEntry;

            if (meta.TryGetValue("ClientId", out clientIdEntry))
            {
                if (clientIdEntry != null)
                {
                    if (!int.TryParse(clientIdEntry.ToString(), out clientId))
                    {
                        clientId = 1;
                    }
                }
            }

            using (var connection = new SqlConnection(connectionString))
                using (var command = connection.CreateCommand("cspAppExceptionInsert"))
                {
                    command.AddWithValue("@Environment", Environment.MachineName)
                    .AddWithValue("@ApplicationName", "File Service")
                    .AddWithValue("@ApplicationType", "File Service")
                    .AddWithValue("@UserName", Environment.UserName)
                    .AddIfNotEmptyOrDbNull("@Message", message)
                    .AddIfNotEmptyOrDbNull("@StackTrace", exception.StackTrace)
                    .AddIfNotEmptyOrDbNull("@Source", exception.Source)
                    .AddIfNotEmptyOrDbNull("@FormattedMessage", formattedMessage)
                    .AddIfNotEmptyOrDbNull("@StackHash", stackHash)
                    .AddWithValue("@PostDate", DateTime.Now)
                    .AddWithValue("@ClientID", clientId);

                    if (connection.State != ConnectionState.Open)
                    {
                        connection.Open();
                    }

                    command.ExecuteNonQuery();
                }
        }
Esempio n. 2
0
        public void PublishMessage(Exception exception, string message, IDictionary <object, object> meta)
        {
            var directoryPath = ConfigurationManager.AppSettings["ExceptionLogDirectory"];

            if (string.IsNullOrEmpty(directoryPath))
            {
                return;
            }

            var directoryInfo = new DirectoryInfo(directoryPath);

            if (!directoryInfo.Exists)
            {
                directoryInfo.Create();
            }

            if (exception is AggregateException)
            {
                exception = exception.InnerException;
            }

            var clientId         = 1;
            var clientName       = "IH";
            var formattedMessage = ExceptionFormatter.FormatText(exception, meta);

            object clientIdEntry;

            if (meta.TryGetValue("ClientId", out clientIdEntry))
            {
                if (clientIdEntry != null)
                {
                    if (!int.TryParse(clientIdEntry.ToString(), out clientId))
                    {
                        clientId = 1;
                    }
                }
            }

            object clientNameEntry;

            if (meta.TryGetValue("ClientName", out clientNameEntry))
            {
                if (clientNameEntry != null)
                {
                    clientName = clientNameEntry.ToString();
                }
            }

            var clientFileName = filePattern
                                 .Replace("{ClientId}", clientId.ToString())
                                 .Replace("{ClientName}", clientName);

            var fileExtension = Path.GetExtension(clientFileName);
            var fileName      = Path.GetFileNameWithoutExtension(clientFileName);
            var fileDate      = GetFileDate();

            fileName = (string.IsNullOrEmpty(fileDate))
                           ? string.Concat(fileName, fileExtension)
                           : string.Concat(fileName, "_", fileDate, fileExtension);

            var filePath = Path.Combine(directoryInfo.FullName, fileName);
            var fileInfo = new FileInfo(filePath);

            using (var stream = fileInfo.Open(FileMode.Append, FileAccess.Write, FileShare.Read))
                using (var writer = new StreamWriter(stream))
                {
                    writer.Write(formattedMessage);
                }
        }