-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from dotnet-campus/t/lindexi/LoggerInterpolate…
…dStringHandler 尝试减少日志过程中的字符串创建数量
- Loading branch information
Showing
17 changed files
with
329 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/dotnetCampus.Logger/LoggerInterpolatedStringHandler.g.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#if NET6_0_OR_GREATER | ||
using global::System.Runtime.CompilerServices; | ||
|
||
namespace dotnetCampus.Logging; | ||
|
||
[InterpolatedStringHandler] | ||
public ref struct LoggerInterpolatedStringHandler | ||
{ | ||
public LoggerInterpolatedStringHandler(int literalLength, int formattedCount) | ||
{ | ||
_handler = new DefaultInterpolatedStringHandler(literalLength, formattedCount); | ||
} | ||
|
||
private readonly DefaultInterpolatedStringHandler _handler; | ||
|
||
public void AppendLiteral(string s) => _handler.AppendLiteral(s); | ||
|
||
public void AppendFormatted<T>(T value) => _handler.AppendFormatted(value); | ||
|
||
public void AppendFormatted<T>(T value, string format) => _handler.AppendFormatted(value, format); | ||
|
||
public string ToStringAndClear() => _handler.ToStringAndClear(); | ||
|
||
public override string ToString() => _handler.ToString(); | ||
|
||
/// <summary> | ||
/// 废弃掉此字符串 | ||
/// </summary> | ||
public void Discard() | ||
{ | ||
// 这里的 ToStringAndClear 其实只是取其 Clear 的功能 | ||
// 暂时先使用 DefaultInterpolatedStringHandler 提供的能力,后续再考虑是否需要优化 | ||
_handler.ToStringAndClear(); | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
tests/dotnetCampus.Logger.Analyzer.Tests/Generators/LoggerBridgeGeneratorTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using dotnetCampus.Logger.Generators; | ||
|
||
using Microsoft.CodeAnalysis.CSharp; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace dotnetCampus.Logger.Analyzer.Tests.Generators; | ||
|
||
[TestClass] | ||
public class LoggerBridgeGeneratorTest | ||
{ | ||
[TestMethod] | ||
public void TestGenerateLoggerBridge() | ||
{ | ||
var loggerBridgeGenerator = new LoggerBridgeGenerator(); | ||
GeneratorDriver driver = CSharpGeneratorDriver.Create(loggerBridgeGenerator); | ||
|
||
var compilation = CSharpCompilation.Create | ||
( | ||
"Test", | ||
syntaxTrees: | ||
[ | ||
CSharpSyntaxTree.ParseText | ||
( | ||
""" | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using dotnetCampus.Logging.Attributes; | ||
using dotnetCampus.Logging.Configurations; | ||
using dotnetCampus.Logging.Writers; | ||
|
||
namespace LoggerSample.MainApp; | ||
|
||
[ImportLoggerBridge<global::LoggerSample.LoggerIndependentLibrary.Logging.ILoggerBridge>] | ||
[ImportLoggerBridge<global::LoggerSample.LoggerIndependentProject.Logging.ILoggerBridge>] | ||
internal partial class LoggerBridgeLinker; | ||
""" | ||
) | ||
], | ||
references: | ||
new[] | ||
{ | ||
MetadataReference.CreateFromFile(typeof(LoggerSample.LoggerIndependentProject.SourceReferenceTarget).Assembly.Location), | ||
MetadataReference.CreateFromFile(typeof(LoggerSample.LoggerIndependentLibrary.SourceReferenceTarget).Assembly.Location) | ||
} // 加上整个 dotnet 的基础库 | ||
.Concat(MetadataReferenceProvider.GetDotNetMetadataReferenceList()) | ||
); | ||
|
||
driver = driver.RunGenerators(compilation); | ||
var result = driver.GetRunResult(); | ||
Assert.IsNotNull(result); | ||
} | ||
} |
Oops, something went wrong.