-
Notifications
You must be signed in to change notification settings - Fork 245
Debug logger #207
Debug logger #207
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Diagnostics; | ||
using System.Text; | ||
|
||
namespace Microsoft.Framework.Logging.Debug | ||
{ | ||
public class DebugLogger : ILogger | ||
{ | ||
private readonly Func<string, LogLevel, bool> _filter; | ||
private readonly string _name; | ||
|
||
public DebugLogger(string name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add doc comments. |
||
: this(name, filter: null) | ||
{ | ||
} | ||
|
||
public DebugLogger(string name, Func<string, LogLevel, bool> filter) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add doc comments. |
||
{ | ||
_name = string.IsNullOrEmpty(name) ? nameof(DebugLogger) : name; | ||
_filter = filter ?? ((category, logLevel) => true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should just leave |
||
} | ||
|
||
public IDisposable BeginScopeImpl(object state) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add doc comments. |
||
{ | ||
return null; | ||
} | ||
|
||
public bool IsEnabled(LogLevel logLevel) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add doc comments. |
||
{ | ||
if (!Debugger.IsAttached) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem correct There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah this seems like it could be simplified to:
|
||
{ | ||
return false; | ||
} | ||
|
||
return _filter(_name, logLevel); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per comment above: return _filter == null || _filter(_name, logLevel); |
||
} | ||
|
||
public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add doc comments (can possibly just |
||
{ | ||
if (!IsEnabled(logLevel)) | ||
{ | ||
return; | ||
} | ||
|
||
string message; | ||
var values = state as ILogValues; | ||
if (formatter != null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That attribute throws if the value is null, here it is actually optional. |
||
{ | ||
message = formatter(state, exception); | ||
} | ||
else if (values != null) | ||
{ | ||
message = LogFormatter.FormatLogValues(values); | ||
if (exception != null) | ||
{ | ||
message += Environment.NewLine + exception; | ||
} | ||
} | ||
else | ||
{ | ||
message = LogFormatter.Formatter(state, exception); | ||
} | ||
|
||
if (string.IsNullOrEmpty(message)) | ||
{ | ||
return; | ||
} | ||
|
||
message = $"{ logLevel }: {message}"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. take a look at the following recently merged in external PR and see if we should do something like that for log level: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: extra whitespace in the placeholders here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kichalla that doesn't make as much sense here as we're passing the |
||
System.Diagnostics.Debug.WriteLine(message, _name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. intentional that you excluded writing out There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to fully qualify, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I knew I should have put a comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah got it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❗ Ummmmm.......... this won't work, like ever, will it? I think @davidfowl and I once came up with a trick to make this work by putting the code in a separate file and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@Eilon for my own understanding, this logger is supposed(like the template will probably add this provider within the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I think I got what you are saying...just tried in a sample app(in debug mode) referencing this package and since this package is built for Release, my log statement never spitted out the log statements(probably because of the reason you mentioned)..am i right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but this package is always compiled in release mode, and will never have the call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I was initially misled into believing this works as it is when I used the sample app present in Logging solution's samples and because the whole solution is built for debug it seemed working as it supposed to. |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.Framework.Logging.Debug; | ||
|
||
namespace Microsoft.Framework.Logging | ||
{ | ||
public static class DebugLoggerFactoryExtensions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add doc comments. |
||
{ | ||
/// <summary> | ||
/// Adds a debug logger that is enabled for <see cref="LogLevel"/>.Information or higher. | ||
/// </summary> | ||
public static ILoggerFactory AddDebug(this ILoggerFactory factory) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add doc comments for parameters. |
||
{ | ||
return AddDebug( | ||
factory, | ||
(category, logLevel) => logLevel >= LogLevel.Information); | ||
} | ||
|
||
/// <summary> | ||
/// Adds a debug logger that is enabled as defined by the filter function. | ||
/// </summary> | ||
public static ILoggerFactory AddDebug(this ILoggerFactory factory, Func<string, LogLevel, bool> filter) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add doc comments for parameters. |
||
{ | ||
factory.AddProvider(new DebugLoggerProvider(filter)); | ||
return factory; | ||
} | ||
|
||
/// <summary> | ||
/// Adds a debug logger that is enabled for <see cref="LogLevel"/>s of minLevel or higher. | ||
/// </summary> | ||
/// <param name="minLevel">The minimum <see cref="LogLevel"/> to be logged</param> | ||
public static ILoggerFactory AddDebug(this ILoggerFactory factory, LogLevel minLevel) | ||
{ | ||
return AddDebug( | ||
factory, | ||
(category, logLevel) => logLevel >= minLevel); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: rename |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
|
||
namespace Microsoft.Framework.Logging.Debug | ||
{ | ||
public class DebugLoggerProvider : ILoggerProvider | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add doc comments. |
||
{ | ||
private readonly Func<string, LogLevel, bool> _filter; | ||
|
||
public DebugLoggerProvider(Func<string, LogLevel, bool> filter) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add doc comments. |
||
{ | ||
_filter = filter; | ||
} | ||
|
||
public ILogger CreateLogger(string name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add doc comments. |
||
{ | ||
return new DebugLogger(name, _filter); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> | ||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> | ||
</PropertyGroup> | ||
|
||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" /> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>ffedc225-d5bd-44e0-b7a6-a98fcfecc694</ProjectGuid> | ||
<RootNamespace>Microsoft.Framework.Logging.Debug</RootNamespace> | ||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath> | ||
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" /> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"version": "1.0.0-*", | ||
"dependencies": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a description to this. |
||
"Microsoft.Framework.Logging.Abstractions": "1.0.0-*" | ||
}, | ||
|
||
"frameworks": { | ||
"net45": { | ||
}, | ||
"dnx451": { | ||
}, | ||
"dotnet": { | ||
"dependencies": { | ||
"System.Collections": "4.0.10-beta-*", | ||
"System.Globalization": "4.0.10-beta-*", | ||
"System.Linq": "4.0.0-beta-*" | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add doc comments.