gpt4 book ai didi

dynamics-crm-2011 - 为 Dynamics CRM 插件登录生产的策略

转载 作者:行者123 更新时间:2023-12-04 09:00:44 25 4
gpt4 key购买 nike

CRM 2011 内部部署。

我有一个用 C# 编写的插件。它可能会抛出异常或在生产中表现不佳。

发生这种情况时,我想捕获有关状态和最近代码执行的信息,以帮助我分析问题。

理想情况下,我想要以下内容:

  • 如果代码决定我应该知道某个问题,那么我希望它能够尽快告诉我存在问题,而无需我查看是否发生了问题。
  • 我希望我可以轻松访问有关该问题的信息。我不想 RDP 到另一台机器并搜索文件。
  • 我不希望日志记录对性能产生太大影响。

  • 我想我正在寻找这样的东西:
  • 保留内存中的最后 n 行日志。
  • 向日志添加一行的单个函数调用。
  • 导致记录错误的单个函数调用。
  • 当出现错误时,给我发电子邮件说有问题。这包含摘要信息。
  • 该电子邮件包含一个指向网页的链接,该网页向我展示了完整的错误详细信息。堆栈跟踪、错误消息、日期、时间、用户等,日志的最后 n 行。
  • 如果某处也有一个网页向我显示所有错误,包括过滤、排序等,那就太好了。

  • 我对 CRM 还很陌生,但我以前开发过这样的系统。由于 CRM 已经存在多年,我希望它可以使用它。

    最佳答案

    为了支持您的愿望 list ,我将创建这些实体:

    插件日志

  • 包含您希望为成功完成的插件调用保留的所有信息
  • 与插件异常类相关。这样你就可以查到之前发生的事情
    发生异常

  • 插件异常
  • 包含有关异常的任何特殊信息(用户、上下文、堆栈跟踪)

  • 现在让我们来看看你的愿望 list :
  • 保留内存中的最后 n 行日志。
  • 不确定是否要记录特定的插件类或 DLL 中定义的所有插件类,我将假设一个特定的插件类:
  • 为插件创建一个静态的 ConcurrentQueue
  • 向日志添加一行的单个函数调用。
  • 创建一个函数,在内存中创建一个 PluginLog 实体(而不是在 CRM 数据库中创建它)并将其添加到队列中。
  • 如果长度> n,则出队。
  • 导致记录错误的单个函数调用。
  • 同样,这是您需要创建的内容。基本上我会在 CRM 中创建一个 PLuginException 实体,然后将所有项目从队列中取出,填充插件异常 ID,并将其保存到 CRM
  • 当出现错误时,给我发电子邮件说有问题。这包含摘要信息。
  • 只要执行插件的 App Domain 上下文具有所需的权限(不确定它是否在 CRM Online 中),这应该是微不足道的。
  • 该电子邮件包含一个指向网页的链接,该网页向我展示了完整的错误详细信息。堆栈跟踪、错误消息、日期、时间、用户等,日志的最后 n 行。
  • 您可以创建一个指向 PluginException Entity Created 的链接,并将其与所有其他相关信息一起包含在电子邮件中。
  • 如果某处也有一个网页向我显示所有错误,包括过滤、排序等,那就太好了。
  • 高级查找救援

  • 编辑

    为了帮助您入门,这是我目前用来从插件上下文中检索所有信息并将其转换为插入异常的文本的方法:

        #region GetPluginInfo

    private Exception GetPluginExecutionInfoForLog(IServiceProvider serviceProvider, Exception ex)
    {
    if(ex.GetType() == typeof(InvalidPluginExecutionException)){ return ex; }

    try
    {
    var context = serviceProvider.GetContext();

    ex = new InvalidPluginExecutionException(
    String.Format("Error During Plugin Execution: {0}**** Context Values ****{0}{1}",
    Environment.NewLine, GetPluginExecutionInfo(context)), ex);
    }
    catch (Exception childEx)
    {
    OnError(childEx);
    }
    return ex;
    }

    protected String GetPluginExecutionInfo(IPluginExecutionContext context)
    {
    var lines = new List<String>();
    var target = GetTarget<Entity>(context);

    lines.Add("MessageName: " + context.MessageName);
    lines.Add("PrimaryEntityName: " + context.PrimaryEntityName);
    lines.Add("PrimaryEntityId: " + context.PrimaryEntityId);
    lines.Add("BusinessUnitId: " + context.BusinessUnitId);
    lines.Add("CorrelationId: " + context.CorrelationId);
    lines.Add("Depth: " + context.Depth);
    lines.Add("Has Parent Context: " + (context.ParentContext != null));
    lines.Add("InitiatingUserId: " + context.InitiatingUserId);
    AddParameters(lines, context.InputParameters, "Input Parameters");
    lines.Add("IsInTransaction: " + context.IsInTransaction);
    lines.Add("IsolationMode: " + context.IsolationMode);
    lines.Add("Mode: " + context.Mode);
    lines.Add("OperationCreatedOn: " + context.OperationCreatedOn);
    lines.Add("OperationId: " + context.OperationId);
    lines.Add("Organization: " + context.OrganizationName + "(" + context.OrganizationId + ")");
    AddParameters(lines, context.OutputParameters, "Output Parameters");
    AddEntityReference(lines, context.OwningExtension, "OwningExtension");
    AddEntityImages(lines, context.PostEntityImages, "Post Entity Images");
    AddEntityImages(lines, context.PreEntityImages, "Pre Entity Images");
    lines.Add("SecondaryEntityName: " + context.SecondaryEntityName);
    AddParameters(lines, context.SharedVariables, "Shared Variables");
    lines.Add("Stage: " + context.Stage);
    lines.Add("UserId: " + context.UserId);

    if (target == null || target.Attributes.Count == 0)
    {
    lines.Add("Target: Empty ");
    }
    else
    {
    lines.Add("* Target " + target.ToEntityReference().GetNameId() + " *");
    foreach (var att in target.Attributes)
    {
    lines.Add(" Entity[" + att.Key + "]: " + GetAttributeValue(att.Value));
    }
    }

    lines.Add("* App Config Values *");
    foreach (var key in ConfigurationManager.AppSettings.AllKeys)
    {
    lines.Add(" [" + key + "]: " + ConfigurationManager.AppSettings[key]);
    }

    return String.Join(Environment.NewLine, lines);
    }

    private static string GetAttributeValue(object value)
    {
    if(value == null){
    return "Null";
    }
    var type = value.GetType();
    if (type == typeof(OptionSetValue))
    {
    return ((OptionSetValue)value).Value.ToString();
    }
    else if (type == typeof(EntityReference))
    {
    return ((EntityReference)value).GetNameId();
    }
    else
    {
    return value.ToString();
    }
    }

    private static void AddEntityReference(List<string> nameValuePairs, EntityReference entity, string name)
    {
    if (entity != null)
    {
    nameValuePairs.Add(name + ": " + entity.GetNameId());
    }
    }

    private static void AddEntityImages(List<string> nameValuePairs, EntityImageCollection images, string name)
    {
    if (images != null && images.Count > 0)
    {
    nameValuePairs.Add("** " + name + " **");
    foreach (var image in images)
    {
    if (image.Value == null || image.Value.Attributes.Count == 0)
    {
    nameValuePairs.Add(" Image[" + image.Key + "] " + image.Value.ToEntityReference().GetNameId() + ": Empty");
    }
    else
    {
    nameValuePairs.Add("* Image[" + image.Key + "] " + image.Value.ToEntityReference().GetNameId() + " *");
    foreach (var att in image.Value.Attributes)
    {
    nameValuePairs.Add(" Entity[" + att.Key + "]: " + GetAttributeValue(att.Value));
    }
    }
    }
    }
    else
    {
    nameValuePairs.Add(name + ": Empty");
    }
    }

    private static void AddParameters(List<string> nameValuePairs, ParameterCollection parameters, string name)
    {
    if (parameters != null && parameters.Count > 0)
    {
    nameValuePairs.Add("* " + name + " *");
    foreach (var param in parameters)
    {
    nameValuePairs.Add(" Param[" + param.Key + "]: " + param.Value);
    }
    }
    else
    {
    nameValuePairs.Add(name + ": Empty");
    }
    }

    #endregion // GetPluginInfo

    关于dynamics-crm-2011 - 为 Dynamics CRM 插件登录生产的策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19049514/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com