Compare commits
2 Commits
613b11e88c
...
f2cdfc86b7
| Author | SHA1 | Date | |
|---|---|---|---|
| f2cdfc86b7 | |||
| fa9c5ac689 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -18,6 +18,7 @@ nuget/
|
||||
.idea/
|
||||
.editorconfig
|
||||
*.user
|
||||
*.DotSettings
|
||||
|
||||
#backups
|
||||
.old*/
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
<PropertyGroup>
|
||||
<!--package info-->
|
||||
<PackageId>DTLib.Logging.Microsoft</PackageId>
|
||||
<Version>1.1.0</Version>
|
||||
<Version>1.1.1</Version>
|
||||
<Authors>Timerix</Authors>
|
||||
<Description>DTLib logger wrapper with dependency injection</Description>
|
||||
<RepositoryType>GIT</RepositoryType>
|
||||
@ -28,6 +28,6 @@
|
||||
<ProjectReference Include="..\DTLib\DTLib.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup Condition=" '$(Configuration)' != 'Debug' ">
|
||||
<PackageReference Include="DTLib" Version="1.4.*" />
|
||||
<PackageReference Include="DTLib" Version="1.6.*" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
<PropertyGroup>
|
||||
<!--package info-->
|
||||
<PackageId>DTLib</PackageId>
|
||||
<Version>1.6.0</Version>
|
||||
<Version>1.6.1</Version>
|
||||
<Authors>Timerix</Authors>
|
||||
<Description>Library for all my C# projects</Description>
|
||||
<RepositoryType>GIT</RepositoryType>
|
||||
|
||||
@ -1,3 +0,0 @@
|
||||
<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">
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=console/@EntryIndexedValue">False</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=logging_005Cloggers/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
||||
@ -2,23 +2,16 @@ namespace DTLib.Logging;
|
||||
|
||||
public class DefaultLogFormat : ILogFormat
|
||||
{
|
||||
|
||||
public bool PrintTimeStamp { get; set; }
|
||||
public bool PrintContext { get; set; }
|
||||
public bool PrintSeverity { get; set; }
|
||||
|
||||
public DefaultLogFormat(bool printTimeStamp = true, bool printContext = true, bool printSeverity = true)
|
||||
{
|
||||
PrintTimeStamp = printTimeStamp;
|
||||
PrintContext = printContext;
|
||||
PrintSeverity = printSeverity;
|
||||
}
|
||||
public bool PrintTimeStamp { get; set; } = true;
|
||||
public bool PrintContext { get; set; } = true;
|
||||
public bool PrintSeverity { get; set; } = true;
|
||||
public string TimeStampFormat { get; set; } = MyTimeFormat.ForText;
|
||||
|
||||
public string CreateMessage(string context, LogSeverity severity, object message)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
if (PrintTimeStamp)
|
||||
sb.Append('[').Append(DateTime.Now.ToString(MyTimeFormat.ForText)).Append(']');
|
||||
sb.Append('[').Append(DateTime.Now.ToString(TimeStampFormat)).Append(']');
|
||||
if (PrintContext && PrintSeverity)
|
||||
sb.Append('[').Append(context).Append('/').Append(severity.ToString()).Append(']');
|
||||
else if(PrintContext)
|
||||
|
||||
@ -2,9 +2,5 @@ namespace DTLib.Logging;
|
||||
|
||||
public interface ILogFormat
|
||||
{
|
||||
bool PrintTimeStamp { get; set; }
|
||||
bool PrintContext { get; set; }
|
||||
bool PrintSeverity { get; set; }
|
||||
|
||||
string CreateMessage(string context, LogSeverity severity, object message);
|
||||
}
|
||||
@ -4,4 +4,6 @@ public static class MyTimeFormat
|
||||
{
|
||||
public const string ForFileNames="yyyy.MM.dd_HH-mm-ss_zz";
|
||||
public const string ForText="yyyy.MM.dd HH:mm:ss zz";
|
||||
public const string TimeOnly="HH:mm:ss";
|
||||
public const string DateOnly="yyyy.MM.dd";
|
||||
}
|
||||
69
DTLib/TransformStream.cs
Normal file
69
DTLib/TransformStream.cs
Normal file
@ -0,0 +1,69 @@
|
||||
using System.IO;
|
||||
using File = System.IO.File;
|
||||
|
||||
namespace DTLib;
|
||||
|
||||
/// <summary>
|
||||
/// Stream wrapper for some operations over data chunks.
|
||||
/// You can add multiple transform operations.
|
||||
/// <example><code>
|
||||
/// using var pipe = new TransformStream(File.OpenRead("encrypted"))
|
||||
/// .AddTransform(ReportProgress)
|
||||
/// .AddTransform(Decrypt);
|
||||
/// using var o = File.OpenWrite("decrypted");
|
||||
/// pipe.CopyTo(o);
|
||||
/// </code></example>
|
||||
/// </summary>
|
||||
public class TransformStream : Stream
|
||||
{
|
||||
private readonly Stream _inputStream;
|
||||
|
||||
public delegate void TransformFuncDelegate(byte[] buffer, int offset, int count);
|
||||
private List<TransformFuncDelegate> _transformFunctions = new();
|
||||
|
||||
public TransformStream(Stream inputStream)
|
||||
{
|
||||
_inputStream = inputStream;
|
||||
}
|
||||
|
||||
public TransformStream AddTransform(TransformFuncDelegate f)
|
||||
{
|
||||
_transformFunctions.Add(f);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TransformStream AddTransforms(IEnumerable<TransformFuncDelegate> f)
|
||||
{
|
||||
_transformFunctions.AddRange(f);
|
||||
return this;
|
||||
}
|
||||
|
||||
public override bool CanRead => _inputStream.CanRead;
|
||||
public override bool CanSeek => _inputStream.CanSeek;
|
||||
public override bool CanWrite => false;
|
||||
public override long Length => _inputStream.Length;
|
||||
public override long Position { get => _inputStream.Position; set => _inputStream.Position = value; }
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
int read = _inputStream.Read(buffer, offset, count);
|
||||
if(read > 0)
|
||||
{
|
||||
for (int i = 0; i < _transformFunctions.Count; i++)
|
||||
{
|
||||
_transformFunctions[i].Invoke(buffer, offset, read);
|
||||
}
|
||||
}
|
||||
return read;
|
||||
}
|
||||
|
||||
public override void Flush() {}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin) => _inputStream.Seek(offset, origin);
|
||||
|
||||
public override void SetLength(long value) => throw new NotImplementedException();
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count) => throw new NotImplementedException();
|
||||
|
||||
public override void Close() => _inputStream.Close();
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user