Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
L
LcdLogger
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Antoni Zaprzała
LcdLogger
Commits
276a4432
Commit
276a4432
authored
Jul 01, 2026
by
AntekZ
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
endpoints added
parent
104eeacd
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
123 additions
and
8 deletions
+123
-8
LogFileInfo.cs
LcdLogger/Models/LogFileInfo.cs
+8
-0
Program.cs
LcdLogger/Program.cs
+27
-5
LogFileBrowser.cs
LcdLogger/Services/LogFileBrowser.cs
+85
-0
LogFileManager.cs
LcdLogger/Services/LogFileManager.cs
+3
-3
No files found.
LcdLogger/Models/LogFileInfo.cs
0 → 100644
View file @
276a4432
namespace
LcdLogger.Models
{
public
sealed
class
LogFileInfo
{
public
string
Name
{
get
;
set
;
}
public
long
SizeBytes
{
get
;
set
;
}
}
}
LcdLogger/Program.cs
View file @
276a4432
using
LcdLogger.Models
;
using
Microsoft.Extensions.Options
;
using
LcdLogger.Services
;
using
Microsoft.Extensions.Options
;
using
System.Xml.Linq
;
var
builder
=
WebApplication
.
CreateBuilder
(
args
);
builder
.
Services
.
Configure
<
LogOptions
>(
builder
.
Configuration
.
GetSection
(
"Log"
));
builder
.
Services
.
AddSingleton
<
LogQueue
>();
builder
.
Services
.
AddSingleton
<
LogFileManager
>();
builder
.
Services
.
AddSingleton
<
LogFileBrowser
>();
builder
.
Services
.
AddHostedService
<
LogWorker
>();
var
app
=
builder
.
Build
();
//app.MapGet("/debug/options", (IOptions<LogOptions> options) =>
// Results.Ok(options.Value));
app
.
MapPost
(
"/logs"
,
(
LogBatch
batch
,
LogQueue
queue
,
IOptions
<
LogOptions
>
opts
)
=>
{
if
(
batch
.
Logs
.
Length
>
opts
.
Value
.
MaxBatchEntries
)
...
...
@@ -23,6 +21,30 @@ app.MapPost("/logs", (LogBatch batch, LogQueue queue, IOptions<LogOptions> opts)
return
Results
.
Accepted
();
});
// return all files in json
// http://127.0.0.1:8082/files
app
.
MapGet
(
"/files"
,
(
LogFileBrowser
browser
)
=>
{
return
Results
.
Ok
(
browser
.
List
());
});
// downloads to working directory a single log file by name
// example: curl -OJ http://127.0.0.1:8082/files/download/lcd-2026-07-01_09-18-33.log
app
.
MapGet
(
"/files/download/{name}"
,
(
string
name
,
LogFileBrowser
browser
)
=>
{
var
path
=
browser
.
GetFilePath
(
name
);
if
(
path
is
null
)
return
Results
.
NotFound
();
return
Results
.
File
(
path
,
"application/octet-stream"
,
fileDownloadName
:
name
);
});
// example: curl -OJ http://127.0.0.1:8082/files/download-all
app
.
MapGet
(
"/files/download-all"
,
async
(
LogFileBrowser
browser
,
CancellationToken
ct
)
=>
{
var
zipBytes
=
await
browser
.
BuildZipAsync
(
ct
);
var
zipName
=
$"lcd-logs-
{
DateTime
.
Now
:
yyyy
-
MM
-
dd_HH
-
mm
-
ss
}
.zip"
;
return
Results
.
File
(
zipBytes
,
"application/zip"
,
fileDownloadName
:
zipName
);
});
app
.
Run
();
LcdLogger/Services/LogFileBrowser.cs
0 → 100644
View file @
276a4432
using
LcdLogger.Models
;
using
Microsoft.Extensions.Options
;
using
System.IO.Compression
;
namespace
LcdLogger.Services
{
public
sealed
class
LogFileBrowser
{
private
readonly
LogOptions
_options
;
private
readonly
ILogger
<
LogFileBrowser
>
_logger
;
public
LogFileBrowser
(
IOptions
<
LogOptions
>
options
,
ILogger
<
LogFileBrowser
>
logger
)
{
_options
=
options
.
Value
;
_logger
=
logger
;
}
public
IReadOnlyList
<
LogFileInfo
>
List
()
{
var
directoryInfo
=
new
DirectoryInfo
(
_options
.
Directory
);
if
(!
directoryInfo
.
Exists
)
{
_logger
.
LogError
(
"Directory {Directory} does not exist"
,
_options
.
Directory
);
return
Array
.
Empty
<
LogFileInfo
>();
}
return
directoryInfo
.
GetFiles
(
"lcd-*.log"
)
.
OrderBy
(
f
=>
f
.
Name
)
.
Select
(
f
=>
new
LogFileInfo
{
Name
=
f
.
Name
,
SizeBytes
=
f
.
Length
})
.
ToList
();
}
public
string
?
GetFilePath
(
string
name
)
{
var
fullPath
=
Path
.
Combine
(
_options
.
Directory
,
name
);
return
File
.
Exists
(
fullPath
)
?
fullPath
:
null
;
}
public
async
Task
<
byte
[
]>
BuildZipAsync
(
CancellationToken
cancellationToken
=
default
)
{
var
directoryInfo
=
new
DirectoryInfo
(
_options
.
Directory
);
if
(!
directoryInfo
.
Exists
)
{
_logger
.
LogWarning
(
"Directory {Directory} does not exist"
,
_options
.
Directory
);
return
Array
.
Empty
<
byte
>();
}
using
var
buffer
=
new
MemoryStream
();
using
(
var
zip
=
new
ZipArchive
(
buffer
,
ZipArchiveMode
.
Create
,
leaveOpen
:
true
))
{
foreach
(
var
file
in
directoryInfo
.
GetFiles
(
"lcd-*.log"
).
OrderBy
(
f
=>
f
.
Name
))
{
try
{
var
entry
=
zip
.
CreateEntry
(
file
.
Name
,
CompressionLevel
.
Fastest
);
using
(
var
entryStream
=
entry
.
Open
())
using
(
var
fileStream
=
new
FileStream
(
file
.
FullName
,
FileMode
.
Open
,
FileAccess
.
Read
,
FileShare
.
ReadWrite
))
{
await
fileStream
.
CopyToAsync
(
entryStream
,
cancellationToken
);
}
}
catch
(
Exception
e
)
{
_logger
.
LogError
(
e
,
"Failed to add {FileName} to zip"
,
file
.
Name
);
}
}
}
// zip.Dispose() na końcu tego bloku → zapisuje footer do buffer (MemoryStream, sync OK)
return
buffer
.
ToArray
();
}
}
}
LcdLogger/Services/LogFileManager.cs
View file @
276a4432
...
...
@@ -66,14 +66,14 @@ namespace LcdLogger.Services
private
Task
PruneSurplusFilesAsync
(
CancellationToken
cancellationToken
=
default
)
{
var
D
irectoryInfo
=
new
DirectoryInfo
(
_options
.
Directory
);
if
(!
D
irectoryInfo
.
Exists
)
var
d
irectoryInfo
=
new
DirectoryInfo
(
_options
.
Directory
);
if
(!
d
irectoryInfo
.
Exists
)
{
_logger
.
LogError
(
"Directory {Directory} does not exist"
,
_options
.
Directory
);
return
Task
.
CompletedTask
;
}
var
files
=
D
irectoryInfo
.
GetFiles
(
"lcd-*.log"
).
OrderBy
(
f
=>
f
.
Name
).
ToList
();
var
files
=
d
irectoryInfo
.
GetFiles
(
"lcd-*.log"
).
OrderBy
(
f
=>
f
.
Name
).
ToList
();
long
totalBytes
=
files
.
Sum
(
f
=>
f
.
Length
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment