summaryrefslogtreecommitdiffstats
path: root/source/Janrain.OpenId/TraceUtil.cs
blob: 73fb8e01fb1d5d11348431152cc924093f177715 (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
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using System.Security;

namespace Janrain.OpenId
{
    public class TraceUtil
    {
        
        private static TraceSwitch openIDTraceSwitch;
        private static TraceSwitch openIDPageOutputTraceSwitch;

        // Some permissions are required, but since it's just tracing we silently ignore failures.
        //[SecurityPermission(SecurityAction.Demand, UnmanagedCode=true, ControlEvidence=true)]
        private static void Trace(string message, string category)
        {
            try {
                string messagePrefix = String.Format("[{0}] : ", DateTime.Now.ToString());
                char[] newLine = new char[2];
                newLine[0] = '\r';
                newLine[1] = '\n';
                string[] splitMessage = message.Split(newLine, StringSplitOptions.RemoveEmptyEntries);
                bool isMultiLineMessage = (splitMessage.Length > 1);
                
                if (isMultiLineMessage)
                {
                    foreach (string line in splitMessage)
                    {
                        System.Diagnostics.Trace.WriteLine(messagePrefix + line, category);
                    }
                }
                else
                {
                    System.Diagnostics.Trace.WriteLine(messagePrefix + message, category);
                }            
            }
            catch (SecurityException) {}
        }
        

        public static void ServerTrace(string message)
        {
            Trace(message, "OpenID Server");            
        }

        public static void ServerTrace(NameValueCollection collection)
        {            
            foreach (string key in collection.Keys)
            {
                ServerTrace(String.Format("{0} = '{1}'", key, collection[key]));                
            }            
        }

        public static void ServerTrace(object serializableObject)
        {
            if (serializableObject == null)
            {
                ServerTrace("<NULL>");
            }
            else
            {
                ServerTrace(SerializeToString(serializableObject));
            }
            
        }





        public static void ConsumerTrace(string message)
        {
            Trace(message, "OpenID Consumer");
        }

        public static void ConsumerTrace(NameValueCollection collection)
        {
            foreach (string key in collection.Keys)
            {
                ConsumerTrace(String.Format("{0} = '{1}'", key, collection[key]));
            }
        }

        public static void ConsumerTrace(object serializableObject)
        {
            if (serializableObject == null)
            {
                ConsumerTrace("<NULL>");
            }
            else
            {
                ConsumerTrace(SerializeToString(serializableObject));
            }

        }        
        
        
        public static TraceSwitch Switch
        {
            get
            {
                if (openIDTraceSwitch == null) { openIDTraceSwitch = new TraceSwitch("OpenID", "OpenID Trace Switch"); }                
                return openIDTraceSwitch;
            }
        }
        
        public static bool TracePageOutput
        {
            get
            {
                if (openIDPageOutputTraceSwitch == null) { openIDPageOutputTraceSwitch = new TraceSwitch("OpenIDEntireResponse", "OpenID Trace Switch"); }
                return (((int)openIDPageOutputTraceSwitch.Level) >= 2);
            }
        }
        
       

        /// <summary>
        /// Serialize obj to an xml string.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SerializeToString(object obj)
        {
            XmlSerializer serializer = new XmlSerializer(obj.GetType());
            StringWriter writer = new StringWriter();

            serializer.Serialize(writer, obj);
            string result = writer.ToString();
            writer.Close();
            return result;
        }

        public static string RecursivelyExtractExceptionMessage(Exception ex)
        {
            string message = "Exceptions with inner exceptions extracted:";
            message += Environment.NewLine + "Exception: " + ex.GetType().ToString();
            message += Environment.NewLine + "Exception.Message: " + ex.Message;
            message += Environment.NewLine + "Exception.StackTrace:" + ex.StackTrace;
            message += Environment.NewLine;

            if ((ex.InnerException) != null) { message += RecursivelyExtractExceptionMessage(ex.InnerException); }
            return message;
        }
    }
}