如何对性能计数器类别进行版本控制?

发布于 2024-08-08 08:55:34 字数 534 浏览 2 评论 0原文

我有一个性能计数器类别。此类别中的计数器可能会在我的下一个版本中发生变化,因此当程序启动时,我想检查该类别是否存在以及它是否是正确的版本 - 如果不存在,则创建新类别。我可以通过在帮助字符串中存储 GUID 来做到这一点,但这显然很臭。是否可以使用 .NET API 更干净地完成此操作?

现有的臭版本...

if (PerformanceCounterCategory.Exists(CATEGORY_NAME))
{
    PerformanceCounterCategory c = new PerformanceCounterCategory(CATEGORY_NAME);
    if (c.CategoryHelp != CATEGORY_VERSION)
    {
        PerformanceCounterCategory.Delete(CATEGORY_NAME);
    }
}

if (!PerformanceCounterCategory.Exists(CATEGORY_NAME))
{
      // Create category
}

I have a performance counter category. The counters in this category may change for my next release so when the program starts, I want to check if the category exists and it is the correct version - if not, create the new category. I can do this by storing a GUID in the help string but this is obviously smelly. Is it possible to do this more cleanly with the .NET API?

Existing smelly version...

if (PerformanceCounterCategory.Exists(CATEGORY_NAME))
{
    PerformanceCounterCategory c = new PerformanceCounterCategory(CATEGORY_NAME);
    if (c.CategoryHelp != CATEGORY_VERSION)
    {
        PerformanceCounterCategory.Delete(CATEGORY_NAME);
    }
}

if (!PerformanceCounterCategory.Exists(CATEGORY_NAME))
{
      // Create category
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆光飞翔i 2024-08-15 08:55:34

在我们的系统中,每次应用程序启动时,我们都会检查现有类别。如果未找到,我们将创建该类别。如果存在,我们会将现有类别与我们期望的类别进行比较,如果存在缺失值,则重新创建它(删除、创建)。

var missing = counters
.Where(counter => !PerformanceCounterCategory.CounterExists(counter.Name, CategoryName))
.Count();

if (missing > 0)
{
    PerformanceCounterCategory.Delete(CategoryName);

    PerformanceCounterCategory.Create(
        CategoryName,
        CategoryHelp,
        PerformanceCounterCategoryType.MultiInstance,
        new CounterCreationDataCollection(counters.Select(x => (CounterCreationData)x).ToArray()));
}

In our system, each time the application starts we do a check for the existing category. If it isn't found, we create the category. If it exists, we compare the existing category to what we expect and recreate it (delete, create) if there are missing values.

var missing = counters
.Where(counter => !PerformanceCounterCategory.CounterExists(counter.Name, CategoryName))
.Count();

if (missing > 0)
{
    PerformanceCounterCategory.Delete(CategoryName);

    PerformanceCounterCategory.Create(
        CategoryName,
        CategoryHelp,
        PerformanceCounterCategoryType.MultiInstance,
        new CounterCreationDataCollection(counters.Select(x => (CounterCreationData)x).ToArray()));
}
眼趣 2024-08-15 08:55:34

我认为没有更好的方法。恕我直言,我不认为这是一个糟糕的解决方案。

I don't think there is a better way. IMHO, I don't think this is a terrible solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文