Headless CMS and Content Managment Hub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

67 lines
1.9 KiB

// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace Squidex.Infrastructure.Log
{
public static class Profile
{
private static ILogProfilerSessionProvider sessionProvider;
private sealed class Timer : IDisposable
{
private readonly Stopwatch watch = Stopwatch.StartNew();
private readonly ProfilerSession session;
private readonly string key;
public Timer(ProfilerSession session, string key)
{
this.session = session;
this.key = key;
}
public void Dispose()
{
watch.Stop();
session.Measured(key, watch.ElapsedMilliseconds);
}
}
public static void Init(ILogProfilerSessionProvider provider)
{
sessionProvider = provider;
}
public static IDisposable Method<T>([CallerMemberName] string memberName = null)
{
return Key($"{typeof(T).Name}/{memberName}");
}
public static IDisposable Method(string objectName, [CallerMemberName] string memberName = null)
{
return Key($"{objectName}/{memberName}");
}
public static IDisposable Key(string key)
{
Guard.NotNull(key, nameof(key));
var session = sessionProvider?.GetSession();
if (session == null)
{
return NoopDisposable.Instance;
}
return new Timer(session, key);
}
}
}