Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Last active January 18, 2024 05:58
Show Gist options
  • Save karenpayneoregon/f4ddadfef51462dc86e26361e71ef996 to your computer and use it in GitHub Desktop.
Save karenpayneoregon/f4ddadfef51462dc86e26361e71ef996 to your computer and use it in GitHub Desktop.
Get all pages
@using Microsoft.AspNetCore.Mvc.RazorPages
@using Microsoft.AspNetCore.Routing
public class IndexModel : PageModel
{
private readonly IEnumerable<EndpointDataSource> _endpointSources;
public IEnumerable<RouteEndpoint> EndpointSources { get; set; }
public IndexModel(IEnumerable<EndpointDataSource> endpointSources)
{
_endpointSources = endpointSources;
EndpointSources = _endpointSources
.SelectMany(x => x.Endpoints)
.OfType<RouteEndpoint>();
}
public void OnGet()
{
foreach (RouteEndpoint rep in EndpointSources)
{
Log.Information("{P1} {P2}", rep.RoutePattern.RawText, rep.DisplayName);
}
}
}
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
SetupLogging.Development();
builder.Services.AddRazorPages(op =>
{
op.Conventions.AddFolderRouteModelConvention("/", model =>
{
foreach (var selector in model.Selectors)
{
var pageName = selector.AttributeRouteModel.Template.ToString();
Log.Information("'{P1}'", pageName);
}
});
});
...
}
using Serilog;
using SeriLogLibrary;
using static System.DateTime;
namespace WebApplication1.Classes;
public class SetupLogging
{
public static void Development()
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Console(theme: SeriLogCustomThemes.Theme3())
.WriteTo.File(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "LogFiles", $"{Now.Year}-{Now.Month}-{Now.Day}", "Log.txt"),
rollingInterval: RollingInterval.Infinite,
outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level}] {Message}{NewLine}{Exception}")
.CreateLogger();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment