Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit b18efc0

Browse files
Adding functionality to change log target on the fly; Fixing bugs;
1 parent 4ed7ac1 commit b18efc0

File tree

13 files changed

+278
-282
lines changed

13 files changed

+278
-282
lines changed

src/GitHub.Logging/ConsoleLogAdapter.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,41 @@ namespace GitHub.Unity
55
{
66
class ConsoleLogAdapter : LogAdapterBase
77
{
8-
public ConsoleLogAdapter(string context) : base(context)
9-
{}
10-
11-
private string GetMessage(string level, string message)
8+
private string GetMessage(string level, string context, string message)
129
{
1310
var time = DateTime.Now.ToString("HH:mm:ss.fff tt");
1411
var threadId = Thread.CurrentThread.ManagedThreadId;
15-
return string.Format("{0} {1} [{2,2}] {3} {4}", time, level, threadId, ContextPrefix, message);
12+
return string.Format("{0} {1} [{2,2}] {3} {4}", time, level, threadId, context, message);
1613
}
1714

18-
protected override void OnInfo(string message)
15+
public override void Info(string context, string message)
1916
{
20-
WriteLine("INFO", message);
17+
WriteLine("INFO", context, message);
2118
}
2219

23-
protected override void OnDebug(string message)
20+
public override void Debug(string context, string message)
2421
{
25-
WriteLine("DEBUG", message);
22+
WriteLine("DEBUG", context, message);
2623
}
2724

28-
protected override void OnTrace(string message)
25+
public override void Trace(string context, string message)
2926
{
30-
WriteLine("TRACE", message);
27+
WriteLine("TRACE", context, message);
3128
}
3229

33-
protected override void OnWarning(string message)
30+
public override void Warning(string context, string message)
3431
{
35-
WriteLine("WARN", message);
32+
WriteLine("WARN", context, message);
3633
}
3734

38-
protected override void OnError(string message)
35+
public override void Error(string context, string message)
3936
{
40-
WriteLine("ERROR", message);
37+
WriteLine("ERROR", context, message);
4138
}
4239

43-
private void WriteLine(string level, string message)
40+
private void WriteLine(string level, string context, string message)
4441
{
45-
Console.WriteLine(GetMessage(level, message));
42+
Console.WriteLine(GetMessage(level, context, message));
4643
}
4744
}
4845
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Linq;
3+
4+
namespace GitHub.Unity
5+
{
6+
static class ExceptionExtensions
7+
{
8+
public static string GetExceptionMessage(this Exception ex)
9+
{
10+
var message = ex.Message + Environment.NewLine + ex.StackTrace;
11+
var caller = Environment.StackTrace;
12+
var stack = caller.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
13+
if (stack.Length > 2)
14+
{
15+
message = message + Environment.NewLine + String.Join(Environment.NewLine, stack.Skip(2).ToArray());
16+
}
17+
return message;
18+
}
19+
}
20+
}

src/GitHub.Logging/FileLogAdapter.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,46 @@ class FileLogAdapter : LogAdapterBase
99
private static readonly object lk = new object();
1010
private readonly string filePath;
1111

12-
public FileLogAdapter(string path, string context) : base(context)
12+
public FileLogAdapter(string path)
1313
{
1414
filePath = path;
1515
}
1616

17-
private string GetMessage(string level, string message)
17+
private string GetMessage(string level, string context, string message)
1818
{
1919
var time = DateTime.Now.ToString("HH:mm:ss tt");
2020
var threadId = Thread.CurrentThread.ManagedThreadId;
21-
return string.Format("{0} {1} [{2,2}] {3,-35} {4}{5}", time, level, threadId, ContextPrefix, message, Environment.NewLine);
21+
return string.Format("{0} {1,5} [{2,2}] {3,-35} {4}{5}", time, level, threadId, context, message, Environment.NewLine);
2222
}
2323

24-
protected override void OnInfo(string message)
24+
public override void Info(string context, string message)
2525
{
26-
WriteLine("INFO", message);
26+
WriteLine("INFO", context, message);
2727
}
2828

29-
protected override void OnDebug(string message)
29+
public override void Debug(string context, string message)
3030
{
31-
WriteLine("DEBUG", message);
31+
WriteLine("DEBUG", context, message);
3232
}
3333

34-
protected override void OnTrace(string message)
34+
public override void Trace(string context, string message)
3535
{
36-
WriteLine("TRACE", message);
36+
WriteLine("TRACE", context, message);
3737
}
3838

39-
protected override void OnWarning(string message)
39+
public override void Warning(string context, string message)
4040
{
41-
WriteLine("WARN", message);
41+
WriteLine("WARN", context, message);
4242
}
4343

44-
protected override void OnError(string message)
44+
public override void Error(string context, string message)
4545
{
46-
WriteLine("ERROR", message);
46+
WriteLine("ERROR", context, message);
4747
}
4848

49-
private void WriteLine(string level, string message)
49+
private void WriteLine(string level, string context, string message)
5050
{
51-
Write(GetMessage(level, message));
51+
Write(GetMessage(level, context, message));
5252
}
5353

5454
private void Write(string message)

src/GitHub.Logging/GitHub.Logging.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@
5757
<Reference Include="System.Xml" />
5858
</ItemGroup>
5959
<ItemGroup>
60+
<Compile Include="Extensions\ExceptionExtensions.cs" />
6061
<Compile Include="FileLogAdapter.cs" />
6162
<Compile Include="ILogging.cs" />
6263
<Compile Include="LogAdapterBase.cs" />
64+
<Compile Include="LogFacade.cs" />
6365
<Compile Include="Logging.cs" />
6466
<Compile Include="MultipleLogAdapter.cs" />
6567
<Compile Include="Properties\AssemblyInfo.cs" />
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=extensions/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

src/GitHub.Logging/LogAdapterBase.cs

Lines changed: 6 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -1,194 +1,15 @@
1-
using System;
2-
using System.Linq;
3-
41
namespace GitHub.Unity
52
{
6-
abstract class LogAdapterBase : ILogging
3+
abstract class LogAdapterBase
74
{
8-
protected string ContextPrefix { get; }
9-
10-
protected LogAdapterBase(string context)
11-
{
12-
if (String.IsNullOrEmpty(context))
13-
{
14-
ContextPrefix = string.Empty;
15-
}
16-
else
17-
{
18-
ContextPrefix = string.Format("<{0}>", context);
19-
}
20-
}
21-
22-
protected abstract void OnInfo(string message);
23-
24-
public void Info(string message)
25-
{
26-
OnInfo(message);
27-
}
28-
29-
public void Info(string format, params object[] objects)
30-
{
31-
Info(String.Format(format, objects));
32-
}
33-
34-
public void Info(Exception ex, string message)
35-
{
36-
var exceptionMessage = GetExceptionMessage(ex);
37-
Info(String.Concat(message, Environment.NewLine, (string)exceptionMessage));
38-
}
39-
40-
public void Info(Exception ex)
41-
{
42-
Info(ex, string.Empty);
43-
}
44-
45-
public void Info(Exception ex, string format, params object[] objects)
46-
{
47-
Info(ex, String.Format(format, objects));
48-
}
49-
50-
protected abstract void OnDebug(string message);
51-
52-
public void Debug(string message)
53-
{
54-
#if DEBUG
55-
OnDebug(message);
56-
#endif
57-
}
58-
59-
public void Debug(string format, params object[] objects)
60-
{
61-
#if DEBUG
62-
Debug(String.Format(format, objects));
63-
#endif
64-
}
65-
66-
public void Debug(Exception ex, string message)
67-
{
68-
#if DEBUG
69-
var exceptionMessage = GetExceptionMessage(ex);
70-
Debug(String.Concat(message, Environment.NewLine, (string)exceptionMessage));
71-
#endif
72-
}
73-
74-
public void Debug(Exception ex)
75-
{
76-
#if DEBUG
77-
Debug(ex, string.Empty);
78-
#endif
79-
}
80-
81-
public void Debug(Exception ex, string format, params object[] objects)
82-
{
83-
#if DEBUG
84-
Debug(ex, String.Format(format, objects));
85-
#endif
86-
}
87-
88-
protected abstract void OnTrace(string message);
89-
90-
public void Trace(string message)
91-
{
92-
if (!Logging.TracingEnabled) return;
93-
94-
OnTrace(message);
95-
}
96-
97-
public void Trace(string format, params object[] objects)
98-
{
99-
if (!Logging.TracingEnabled) return;
100-
101-
Trace(String.Format(format, objects));
102-
}
103-
104-
public void Trace(Exception ex, string message)
105-
{
106-
if (!Logging.TracingEnabled) return;
107-
108-
var exceptionMessage = GetExceptionMessage(ex);
109-
Trace(String.Concat(message, Environment.NewLine, (string)exceptionMessage));
110-
}
111-
112-
public void Trace(Exception ex)
113-
{
114-
if (!Logging.TracingEnabled) return;
115-
116-
Trace(ex, string.Empty);
117-
}
118-
119-
public void Trace(Exception ex, string format, params object[] objects)
120-
{
121-
if (!Logging.TracingEnabled) return;
122-
123-
Trace(ex, String.Format(format, objects));
124-
}
125-
126-
protected abstract void OnWarning(string message);
127-
128-
public void Warning(string message)
129-
{
130-
OnWarning(message);
131-
}
132-
133-
public void Warning(string format, params object[] objects)
134-
{
135-
Warning(String.Format(format, objects));
136-
}
137-
138-
public void Warning(Exception ex, string message)
139-
{
140-
var exceptionMessage = GetExceptionMessage(ex);
141-
Warning(String.Concat(message, Environment.NewLine, (string)exceptionMessage));
142-
}
143-
144-
public void Warning(Exception ex)
145-
{
146-
Warning(ex, string.Empty);
147-
}
148-
149-
public void Warning(Exception ex, string format, params object[] objects)
150-
{
151-
Warning(ex, String.Format(format, objects));
152-
}
153-
154-
protected abstract void OnError(string message);
155-
156-
public void Error(string message)
157-
{
158-
OnError(message);
159-
}
160-
161-
public void Error(string format, params object[] objects)
162-
{
163-
Error(String.Format(format, objects));
164-
}
5+
public abstract void Info(string context, string message);
1656

166-
public void Error(Exception ex, string message)
167-
{
168-
var exceptionMessage = GetExceptionMessage(ex);
169-
Error(String.Concat(message, Environment.NewLine, (string)exceptionMessage));
170-
}
7+
public abstract void Debug(string context, string message);
1718

172-
public void Error(Exception ex)
173-
{
174-
Error(ex, string.Empty);
175-
}
9+
public abstract void Trace(string context, string message);
17610

177-
public void Error(Exception ex, string format, params object[] objects)
178-
{
179-
Error(ex, String.Format(format, objects));
180-
}
11+
public abstract void Warning(string context, string message);
18112

182-
private static string GetExceptionMessage(Exception ex)
183-
{
184-
var message = ex.Message + Environment.NewLine + ex.StackTrace;
185-
var caller = Environment.StackTrace;
186-
var stack = caller.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
187-
if (stack.Length > 2)
188-
{
189-
message = message + Environment.NewLine + String.Join(Environment.NewLine, stack.Skip(2).ToArray());
190-
}
191-
return message;
192-
}
13+
public abstract void Error(string context, string message);
19314
}
19415
}

0 commit comments

Comments
 (0)