Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LogEntry.cs « Logging « Library « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 32cd22fad9c8b707f969d38ad3b00dfa23ec8c0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//  Copyright (C) 2018, The Duplicati Team
//  http://www.duplicati.com, info@duplicati.com
//
//  This library is free software; you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as
//  published by the Free Software Foundation; either version 2.1 of the
//  License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful, but
//  WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Collections.Generic;
using System.Linq;

namespace Duplicati.Library.Logging
{
    /// <summary>
    /// Instance of a log entry
    /// </summary>
    public class LogEntry
    {
        /// <summary>
        /// The time when the entry was logged
        /// </summary>
        public DateTime When;

        /// <summary>
        /// The message that was logged
        /// </summary>
        public readonly string Message;

        /// <summary>
        /// The string format arguments, if any
        /// </summary>
        public readonly object[] Arguments;

        /// <summary>
        /// The message level
        /// </summary>
        public LogMessageType Level;

        /// <summary>
        /// The filter tag for the message
        /// </summary>
        public readonly string FilterTag;

        /// <summary>
        /// The filter tag for the message
        /// </summary>
        public readonly string Tag;

        /// <summary>
        /// The message help-id
        /// </summary>
        public readonly string Id;

        /// <summary>
        /// An optional exception
        /// </summary>
        public readonly Exception Exception;

        /// <summary>
        /// The log context data, if any
        /// </summary>
        private Dictionary<string, string> m_logContext;

        /// <summary>
        /// Gets or sets the <see cref="T:Duplicati.Library.Logging.LogEntry"/> with the specified value.
        /// </summary>
        /// <param name="value">The key to use.</param>
        public string this[string key]
        {
            get
            {
                if (m_logContext == null)
                    return null;
                m_logContext.TryGetValue(key, out var s);
                return s;
            }

            set
            {
                if (m_logContext == null)
                    m_logContext = new Dictionary<string, string>();

                m_logContext[key] = value;

            }
        }

        /// <summary>
        /// Gets the keys in this log context
        /// </summary>
        /// <value>The context keys.</value>
        public IEnumerable<string> ContextKeys
        {
            get
            {
                if (m_logContext == null)
                    return new string[0];

                return m_logContext.Keys;
            }
        }

        /// <summary>
        /// Gets the message, formatted with arguments
        /// </summary>
        /// <value>The formatted message.</value>
        public string FormattedMessage
        {
            get
            {
                if (Arguments == null || Arguments.Length == 0)
                    return Message;

                try
                {
                    return string.Format(Message, Arguments);
                }
                catch
                {
                    // Try no to crash ...
                    return string.Format("Error while formating: \"{0}\" with arguments: [{1}]", Message, string.Join(", ", Arguments.Select(x => x == null ? "(null)" : x.ToString())));

                }
            }
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="T:Duplicati.Library.Logging.LogEntry"/> class.
        /// </summary>
        /// <param name="message">The message to use.</param>
        /// <param name="arguments">An optional set of arguments.</param>
        /// <param name="level">The log level.</param>
        /// <param name="tag">The tag to use.</param>
        /// <param name="id">The message ID.</param>
        /// <param name="exception">An optional exception.</param>
        public LogEntry(string message, object[] arguments, LogMessageType level, string tag, string id, Exception exception)
        {
            When = DateTime.Now;
            Message = message;
            Arguments = arguments;
            Level = level;
            Tag = tag;
            Id = id;
            Exception = exception;
            FilterTag = level + "-" + tag + "-" + id;
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:Duplicati.Library.Logging.LogEntry"/>.
        /// </summary>
        /// <returns>A <see cref="T:System.String"/> that represents the current <see cref="T:Duplicati.Library.Logging.LogEntry"/>.</returns>
        public override string ToString()
        {
            return string.Format("{0:yyyy-MM-dd HH:mm:ss zz} - [{1}]: {2}", When.ToLocalTime(), FilterTag, FormattedMessage);
        }

        /// <summary>
        /// Returns the item as a string optionally with exception details
        /// </summary>
        /// <returns>A <see cref="T:System.String"/> that represents the current <see cref="T:Duplicati.Library.Logging.LogEntry"/>.</returns>
        /// <param name="withExceptionDetails">If set to <c>true</c> the result has expanded exception details.</param>
        public string AsString(bool withExceptionDetails)
        {
            return this.ToString() +
                       ((withExceptionDetails && Exception != null)
                        ? Environment.NewLine + Exception.ToString()
                        : string.Empty);
        }
    }
}