Skip to content

About

A utility library for determining the log path across all environments

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

Soenneker.Utils.LogPath

Chooses a writable log-file path for explicit, Azure App Service, GitHub Actions, container, and local application environments.

Installation

dotnet add package Soenneker.Utils.LogPath

Quick start

using Soenneker.Utils.LogPath;

Call the static LogPathUtil methods directly; no dependency-injection registration is required.

Usage

string logPath = await LogPathUtil.Get("application-.log", cancellationToken);

Log.Logger = new LoggerConfiguration()
    .WriteTo.File(logPath, rollingInterval: RollingInterval.Day)
    .CreateLogger();

Get returns a full path for the supplied file name. It does not create or open the log file. Pass a file name rather than a rooted path or a path containing directory traversal segments.

Candidates are evaluated in this order:

  1. LOG_PATH/<fileName> when the LOG_PATH environment variable is set.
  2. Azure App Service's persistent LogFiles directory.
  3. <GITHUB_WORKSPACE>/logs in GitHub Actions.
  4. /var/log/app in another detected container.
  5. <AppContext.BaseDirectory>/logs as the fallback.

The explicit LOG_PATH override is returned as configured; the caller is responsible for making sure that directory exists and is writable. For the environment-derived candidates, the utility creates the directory and falls through when creation is denied or fails with an I/O error. The final application-directory fallback can still throw if that location cannot be created.

Container detection is the asynchronous part of the operation and observes the cancellation token. Resolving a path does not guarantee a later file open will succeed because permissions or the filesystem can change afterward.

About

A utility library for determining the log path across all environments

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages