Änderung

- Ist die Log-Zeile länger als 80 Zeichen, dann wird sie umgebrochen
This commit is contained in:
Eugen Höglinger 2024-06-14 14:55:57 +02:00
parent 1ff67e8926
commit 0149b71907
22 changed files with 329 additions and 14 deletions

Binary file not shown.

View File

@ -3,11 +3,15 @@
"WorkspaceRootPath": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\", "WorkspaceRootPath": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\",
"Documents": [ "Documents": [
{ {
"AbsoluteMoniker": "D:0:0:{E95B9315-E0B9-4DB0-BA06-CAEA7269E922}|LogWriter\\LogWriter.csproj|c:\\cad\\git-workrepository\\visualstudio\\_dll\\logwriter.git\\logwriter\\logwriter.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", "AbsoluteMoniker": "D:0:0:{E95B9315-E0B9-4DB0-BA06-CAEA7269E922}|LogWriter\\LogWriter.csproj|C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\logwriter\\logwriter.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{E95B9315-E0B9-4DB0-BA06-CAEA7269E922}|LogWriter\\LogWriter.csproj|solutionrelative:logwriter\\logwriter.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}" "RelativeMoniker": "D:0:0:{E95B9315-E0B9-4DB0-BA06-CAEA7269E922}|LogWriter\\LogWriter.csproj|solutionrelative:logwriter\\logwriter.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
}, },
{ {
"AbsoluteMoniker": "D:0:0:{9414648E-B3A3-4279-B2E1-F54E35326E93}|Test_LogWriter\\Test_LogWriter.csproj|C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\test_logwriter\\form1.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", "AbsoluteMoniker": "D:0:0:{E95B9315-E0B9-4DB0-BA06-CAEA7269E922}|LogWriter\\LogWriter.csproj|c:\\cad\\git-workrepository\\visualstudio\\_dll\\logwriter.git\\logwriter\\formattinglongstrings.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{E95B9315-E0B9-4DB0-BA06-CAEA7269E922}|LogWriter\\LogWriter.csproj|solutionrelative:logwriter\\formattinglongstrings.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
},
{
"AbsoluteMoniker": "D:0:0:{9414648E-B3A3-4279-B2E1-F54E35326E93}|Test_LogWriter\\Test_LogWriter.csproj|c:\\cad\\git-workrepository\\visualstudio\\_dll\\logwriter.git\\test_logwriter\\form1.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{9414648E-B3A3-4279-B2E1-F54E35326E93}|Test_LogWriter\\Test_LogWriter.csproj|solutionrelative:test_logwriter\\form1.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}" "RelativeMoniker": "D:0:0:{9414648E-B3A3-4279-B2E1-F54E35326E93}|Test_LogWriter\\Test_LogWriter.csproj|solutionrelative:test_logwriter\\form1.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
}, },
{ {
@ -22,7 +26,7 @@
"DocumentGroups": [ "DocumentGroups": [
{ {
"DockedWidth": 200, "DockedWidth": 200,
"SelectedChildIndex": 6, "SelectedChildIndex": 7,
"Children": [ "Children": [
{ {
"$type": "Bookmark", "$type": "Bookmark",
@ -43,27 +47,39 @@
{ {
"$type": "Document", "$type": "Document",
"DocumentIndex": 1, "DocumentIndex": 1,
"Title": "FormattingLongStrings.cs",
"DocumentMoniker": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\LogWriter\\FormattingLongStrings.cs",
"RelativeDocumentMoniker": "LogWriter\\FormattingLongStrings.cs",
"ToolTip": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\LogWriter\\FormattingLongStrings.cs",
"RelativeToolTip": "LogWriter\\FormattingLongStrings.cs",
"ViewState": "AQIAAAMAAAAAAAAAAAAAAOQAAABFAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2024-06-14T12:42:52.903Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 2,
"Title": "Form1.cs", "Title": "Form1.cs",
"DocumentMoniker": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\Test_LogWriter\\Form1.cs", "DocumentMoniker": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\Test_LogWriter\\Form1.cs",
"RelativeDocumentMoniker": "Test_LogWriter\\Form1.cs", "RelativeDocumentMoniker": "Test_LogWriter\\Form1.cs",
"ToolTip": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\Test_LogWriter\\Form1.cs", "ToolTip": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\Test_LogWriter\\Form1.cs",
"RelativeToolTip": "Test_LogWriter\\Form1.cs", "RelativeToolTip": "Test_LogWriter\\Form1.cs",
"ViewState": "AQIAAHUAAAAAAAAAAAAowJcAAAAMAAAA", "ViewState": "AQIAAIEAAAAAAAAAAAAAAJwAAABzAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2024-06-11T12:34:35.414Z", "WhenOpened": "2024-06-11T12:34:35.414Z",
"EditorCaption": "" "EditorCaption": ""
}, },
{ {
"$type": "Document", "$type": "Document",
"DocumentIndex": 2, "DocumentIndex": 3,
"Title": "Form1.cs [Entwurf]", "Title": "Form1.cs [Entwurf]",
"DocumentMoniker": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\Test_LogWriter\\Form1.cs", "DocumentMoniker": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\Test_LogWriter\\Form1.cs",
"RelativeDocumentMoniker": "Test_LogWriter\\Form1.cs", "RelativeDocumentMoniker": "Test_LogWriter\\Form1.cs",
"ToolTip": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\Test_LogWriter\\Form1.cs [Entwurf]", "ToolTip": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\Test_LogWriter\\Form1.cs [Entwurf]",
"RelativeToolTip": "Test_LogWriter\\Form1.cs [Entwurf]", "RelativeToolTip": "Test_LogWriter\\Form1.cs [Entwurf]",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2024-06-11T12:34:35.436Z", "WhenOpened": "2024-06-11T12:34:35.436Z"
"EditorCaption": " [Entwurf]"
}, },
{ {
"$type": "Document", "$type": "Document",
@ -73,7 +89,7 @@
"RelativeDocumentMoniker": "LogWriter\\LogWriter.cs", "RelativeDocumentMoniker": "LogWriter\\LogWriter.cs",
"ToolTip": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\LogWriter\\LogWriter.cs", "ToolTip": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\LogWriter\\LogWriter.cs",
"RelativeToolTip": "LogWriter\\LogWriter.cs", "RelativeToolTip": "LogWriter\\LogWriter.cs",
"ViewState": "AQIAAAAAAAAAAAAAAAAAAI8AAAAIAAAA", "ViewState": "AQIAABUCAAAAAAAAAAAAADECAAB0AAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2024-06-11T12:34:36.369Z", "WhenOpened": "2024-06-11T12:34:36.369Z",
"EditorCaption": "" "EditorCaption": ""

View File

@ -0,0 +1,270 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Eugen.ESystem.Text
{
public class FormattingLongStrings
{
#region Version und Copyright
// Version und Copyright
static string dllName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //Den Programmnamen auslesen
static string dllVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
static object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
static string copyright = GenerateCopyright();
//Assembly Datum und Zeit
static DateTime value = AssemblyDateTime();
static string date = value.ToShortDateString();
static string time = value.ToLongTimeString();
private static DateTime AssemblyDateTime()
{
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
var buildDateTime = new DateTime(2000, 1, 1).Add(new TimeSpan(TimeSpan.TicksPerDay * version.Build + TimeSpan.TicksPerSecond * 2 * version.Revision));
//Tage seit dem 1. Januar 2000 und Sekunden seit Mitternacht, (multiplizier mit 2 ergibt das Original)
return buildDateTime;
}
private static string CurrentYear()
{
DateTime dtn = DateTime.Now;
return dtn.Year.ToString();
}
private static string StartYear()
{
string startYear = "";
if (((AssemblyCopyrightAttribute)attributes[0]).Copyright.Contains("Copyright © "))
{
startYear = ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace("Copyright © ", "");
while (startYear.StartsWith(" "))
{
startYear = startYear.Remove(0, 1);
}
if (startYear.Contains("-"))
{
startYear = startYear.Remove(startYear.IndexOf("-"));
}
else
{
startYear = startYear.Remove(startYear.IndexOf(" "));
}
return startYear;
}
else
{
DateTime dtn = DateTime.Now;
return dtn.Year.ToString();
}
}
private static string GenerateCopyright()
{
string startYear = StartYear();
string currentYear = CurrentYear();
if (startYear == currentYear)
{
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
}
else
{
string temp = ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
string start = temp.Remove(temp.IndexOf(startYear) - 1);
string end = (temp.Remove(0, temp.IndexOf(" by")));
return String.Concat(start, " ", startYear, "-", currentYear, end);
}
}
#endregion
#region DLL-Info
/// <summary>
/// Contains the name of the dll file.
/// </summary>
public static string DllName { private set; get; }
/// <summary>
/// Contains the version of the dll file.
/// </summary>
public static string DllVersion { private set; get; }
/// <summary>
/// Contains the copyright notice.
/// </summary>
public static string Copyright { private set; get; }
/// <summary>
/// Contains the name-, version- and copyright-information of the dll file.
/// </summary>
public static string[] DllInfo { private set; get; }
private static void SetDllInfo()
{
//Name, Version und Copyright setzen
DllName = dllName;
DllVersion = dllVersion;
Copyright = copyright;
string[] dllInfo = new string[3];
dllInfo[0] = dllName;
dllInfo[1] = dllVersion;
dllInfo[2] = copyright;
DllInfo = dllInfo;
}
protected string ProgramName { private set; get; }
protected string ProgramVersion { private set; get; }
protected string ProgramCopyright { private set; get; }
#endregion
static FormattingLongStrings()
{
SetDllInfo();
}
//public FormattingLongStrings()
//{
// SetDllInfo();
//}
/// <summary>
/// Defines the length type
/// </summary>
public enum LengthType
{
LineLength,
IndentLength
}
/// <summary>
/// Splits a longer text into sections with a defined maximum length for each section
/// </summary>
/// <param name="text">Text to be edited</param>
/// <param name="lengthFirstLine">Length of the first line in characters</param>
/// <param name="lengthSubsequentLines">Length of the following line or the indentation in characters</param>
/// <param name="lengthType">Defines whether the line length or the indentation is measured</param>
/// <returns>Returns a string array with individual lines</returns>
public static string[] FormattingString(string text, int lengthFirstLine, int lengthSubsequentLines, LengthType lengthType)
{
// If the text line is longer than the desired first line length, then split it, otherwise it is already ok
if (text.Length > lengthFirstLine)
{
lengthSubsequentLines = LengthSubsequentLines(lengthFirstLine, lengthSubsequentLines, lengthType); // The text length of the other lines is calculated
int indentLength = lengthFirstLine - lengthSubsequentLines; ;
List<string> lines = new List<string>();
lines.Add(CreateLine(text, lengthFirstLine)); // Create the first line in the length of the first line
text = ShortenStringByAlreadyUsed(text, lines[0]); // The text already used is removed from the text
text = RemoveLeadingSpaces(text); // If the text line begins with a space, remove it
string tempText = "";
// If the text line is longer than the desired line length, then split it
while (text.Length > lengthSubsequentLines)
{
tempText = CreateLine(text, lengthSubsequentLines);
// Generate the other lines with the corresponding length
if (lengthType == LengthType.LineLength)
{
lines.Add(tempText); // If the line length of the additional lines
}
else
{
// Line length is the length of the first line minus the length of the indentation
// Spaces in the number of indents are placed in front of the text
lines.Add(tempText.PadLeft(indentLength + tempText.Length, ' '));
}
text = ShortenStringByAlreadyUsed(text, tempText); // The text already used is removed from the text
text = RemoveLeadingSpaces(text); // If the text line begins with a space, remove it
}
// If the text still contains characters, then edit
if (text.Length > 0)
{
if (lengthType == LengthType.LineLength)
{
lines.Add(text); // If the line type is 'Line length' then add
}
else
{
lines.Add(text.PadLeft(indentLength + text.Length, ' ')); // Otherwise, precede with a space and then add
}
}
return lines.ToArray();
}
else
{
string[] value = {text}; // If the text is shorter than the line length, then use it
return value;
}
}
private static int LengthSubsequentLines(int lengthFirstLine, int lengthSubsequentLines, LengthType lengthType)
{
// Returns the length of the subsequent lines, depending on the line type
if (lengthType == LengthType.LineLength)
{
return lengthSubsequentLines; // Line length
}
else
{
return lengthFirstLine - lengthSubsequentLines; // Line length with indentation
}
}
private static string CreateLine(string text, int lineLength)
{
// Returns a text section of the specified length
string tempLine = text.Remove(lineLength);
string tempRemainingLine = text.Remove(0, lineLength);
// If the next part begins with a space, the line ends with a whole word
if (tempRemainingLine.StartsWith(" "))
{
return tempLine; // Then use the line as it is
}
else
{
if (tempLine.Contains(" "))
{
return tempLine.Remove(tempLine.LastIndexOf(" ")); // Otherwise shorten the line up to the last space
}
else
{
return tempLine; // Then use the line as it is
}
}
}
private static string ShortenStringByAlreadyUsed(string text, string usedCharacters)
{
// Remove the piece of text used from the entire text
return text.Replace(usedCharacters, "");
}
private static string RemoveLeadingSpaces(string text)
{
// If the text line begins with a space, remove the space
while (text.StartsWith(" "))
{
text = text.Remove(0, 1);
}
return text;
}
}
}

View File

@ -1,8 +1,10 @@
using System; using Eugen.ESystem.Text;
using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using static Eugen.ESystem.IO.LogWriter; using static Eugen.ESystem.IO.LogWriter;
using static System.Net.Mime.MediaTypeNames;
namespace Eugen.ESystem.IO namespace Eugen.ESystem.IO
{ {
@ -197,6 +199,10 @@ namespace Eugen.ESystem.IO
} }
} }
private int lineLength = 80;
private int indentLengthWithoutLogLevel = 15;
private int indentLengthWithLogLevel = 23;
private void SetDate() private void SetDate()
{ {
DateTime dateTime = DateTime.Now; DateTime dateTime = DateTime.Now;
@ -523,6 +529,17 @@ namespace Eugen.ESystem.IO
private bool WriteLine(string logLevel, DateTime time, int ms, string message) private bool WriteLine(string logLevel, DateTime time, int ms, string message)
{ {
int indentLength = 0;
if (logLevel == "")
{
indentLength = indentLengthWithoutLogLevel;
}
else
{
indentLength = indentLengthWithLogLevel;
}
try try
{ {
StreamWriter writer; StreamWriter writer;
@ -540,8 +557,18 @@ namespace Eugen.ESystem.IO
} }
// Zeile schreiben // Zeile schreiben
writer.WriteLine(message); if (message.Length > lineLength)
{
foreach (string line in FormattingLongStrings.FormattingString(message, lineLength, indentLength, FormattingLongStrings.LengthType.IndentLength))
{
writer.WriteLine(line);
}
}
else
{
writer.WriteLine(message);
}
// Logdatei schließen // Logdatei schließen
writer.Close(); writer.Close();

View File

@ -40,6 +40,7 @@
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="FormattingLongStrings.cs" />
<Compile Include="LogWriter.cs" /> <Compile Include="LogWriter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>

Binary file not shown.

Binary file not shown.

View File

@ -1 +1 @@
e059c56f5aea4ad7e9b0a0f639bec4193ddedaf32239362c2db6cc7c7ffcade6 4c670e3294eecc743f9ea4d797951847f22f5cb8e82909513a5e79cc343002b8

Binary file not shown.

Binary file not shown.

View File

@ -3,6 +3,7 @@ using System.IO;
using System.Reflection; using System.Reflection;
using System.Windows.Forms; using System.Windows.Forms;
using Eugen.ESystem.IO; using Eugen.ESystem.IO;
using Eugen.ESystem.Text;
namespace Test_LogWriter namespace Test_LogWriter
{ {
@ -145,7 +146,7 @@ namespace Test_LogWriter
logWriter3.Delete(); logWriter3.Delete();
logWriter3.WriteHeader(LogWriter.Header.Default); logWriter3.WriteHeader(LogWriter.Header.Default);
logWriter3.WriteMessage("Das ist Test 3"); logWriter3.WriteMessage("Das ist Test 3. Dabei handelt es sich um eine sehr lange Log-Message. Die ist höchstwahrscheinlich länger als die 80 Zeichen, die in eine Zeile geschrieben werden. Darum soll sie auch umgebrochen werden.");
labelResult.Text = String.Concat(labelResult.Text, "\nDie Log-Datei C:\\TMP\\my_logfile3.log wurde geschrieben"); labelResult.Text = String.Concat(labelResult.Text, "\nDie Log-Datei C:\\TMP\\my_logfile3.log wurde geschrieben");
//Variante 4 - Logdateiname wird angegeben und ein Anwender definierter Header wird geschrieben //Variante 4 - Logdateiname wird angegeben und ein Anwender definierter Header wird geschrieben
@ -153,7 +154,7 @@ namespace Test_LogWriter
logWriter4.Delete(); logWriter4.Delete();
logWriter4.WriteHeader(LogWriter.Header.Userdefined); logWriter4.WriteHeader(LogWriter.Header.Userdefined);
logWriter4.WriteMessage("Das ist Test 4", LogWriter.LogLevel.Information, LogWriter.Time.Write); logWriter4.WriteMessage("Das ist Test 4. Was passiert wenn lange kein Leerzeichen kommt. z.B.1234567890123456789012345678901234567890123456789012345678901234567890", LogWriter.LogLevel.Information, LogWriter.Time.Write);
logWriter4.WriteMessage("Eine Zeile ohne Log Level", LogWriter.LogLevel.NoLevel, LogWriter.Time.Write); logWriter4.WriteMessage("Eine Zeile ohne Log Level", LogWriter.LogLevel.NoLevel, LogWriter.Time.Write);
logWriter4.WriteMessage("Und noch eine Zeile mit Log Level", LogWriter.LogLevel.Warning, LogWriter.Time.Write); logWriter4.WriteMessage("Und noch eine Zeile mit Log Level", LogWriter.LogLevel.Warning, LogWriter.Time.Write);
logWriter4.WriteSeparationLine(); logWriter4.WriteSeparationLine();