Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1563 | Anzahl registrierter User: 1895 | Besucher online: 188
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

MessageBox Appender für LOG4NET


Autor: ThomasMentzel
Sprache: C#
Bewertung:
noch nicht bewertet
Anzahl der Aufrufe: 5631
  
Kick it on dotnet-kicks.de  

Beschreibung:

Diese Appender zeigt für die Fehlermeldungen aus log4net eine MessageBox an. Die Klasse wird wie folgt in der app.config eingebunden:






























Abgelegt unter: log4net, MessageBox, Appender.



C#
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
using System;
using System.Text;

namespace Softwareküche.Common
{
    using System.Globalization;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using log4net.Appender;
    using log4net.Core;
    using log4net.Layout;
    using log4net.Util;

    /// <summary>
    /// MessageBox für log4net zum Anzeigen von "Error" und "Fatal" Fehlermeldungen.
    /// </summary>
    [ComVisible(false)]
    public class MessageBoxAppender : AppenderSkeleton
    {
        private PatternLayout _titleLayout;
        private readonly LevelMapping _LevelMapping = new LevelMapping();

        /// <summary>
        /// Überschreibt die Methode zum Anzeigen und durchreichen der Fehlermeldung
        /// </summary>
        /// <param name="loggingEvent">Logging Event der Append Methode</param>
        protected override void Append(LoggingEvent loggingEvent)
        {
            MessageBoxIcon messageBoxIcon = MessageBoxIcon.Information;

            LevelIcon levelIcon = (LevelIcon) this._LevelMapping.Lookup(loggingEvent.Level);

            if (levelIcon != null)
            {
                messageBoxIcon = levelIcon.Icon;
            }

            // Fehlermessage generieren
            string message;
            if (loggingEvent.ExceptionObject != null)
            {
                // Fall eine Exception diese ausgeben,
                message = loggingEvent.ExceptionObject.Message;
            }
            else
            {
                // Ansonsten die ersten 300 Zeichen
                message = this.RenderLoggingEvent(loggingEvent);

                if (message.IndexOf(Environment.NewLine) > 0)
                {
                    message = message.Substring(0, message.IndexOf(Environment.NewLine) - 1);
                }

                if (message.Length > 300)
                {
                    message = message.Substring(0, 300) + " ...";
                }
            }

            // Titel ist der Fehlerlevel
            string title;
            if (this._titleLayout == null)
            {
                title = loggingEvent.Level.Name;
            }
            else
            {
                StringWriter titleWriter = new StringWriter(CultureInfo.InvariantCulture);
                this._titleLayout.Format(titleWriter, loggingEvent);
                title = titleWriter.ToString();
                titleWriter.Dispose();
            }

            // Fehler anzeigen
            MessageBox.Show(message, title, MessageBoxButtons.OK, messageBoxIcon, MessageBoxDefaultButton.Button1, 0);
        }

        /// <summary>
        /// Überschreiben und durchreichen der Methode.
        /// </summary>
        /// <param name="mapping">Das Mapping</param>
        public void AddMapping(LevelIcon mapping)
        {
            this._LevelMapping.Add(mapping);
        }

        /// <summary>
        /// Überschreiben und durchreichen der Methode.
        /// </summary>
        public PatternLayout TitleLayout
        {
            get { return this._titleLayout; }
            set { this._titleLayout = value; }
        }

        /// <summary>
        /// Überschreiben und durchreichen der Methode.
        /// </summary>
        public override void ActivateOptions()
        {
            base.ActivateOptions();
            this._LevelMapping.ActivateOptions();
        }
    }

    /// <summary>
    /// Icon, das angezeigt wird.
    /// </summary>
    [ComVisible(false)]
    public class LevelIcon : LevelMappingEntry
    {
        /// <summary>
        /// Überschreiben und durchreichen der Methode.
        /// </summary>
        public LevelIcon()
            : base()
        {
            this._icon = MessageBoxIcon.None;
        }

        private MessageBoxIcon _icon;

        /// <summary>
        /// Icon für die Message Box
        /// </summary>
        public MessageBoxIcon Icon
        {
            get { return this._icon; }
            set { this._icon = value; }
        }
    }
}
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.