C# 为什么相同的代码在调试版本和发布版本之间呈现不同的结果 编辑:
决定是否继续处理潜在客户的C# 为什么相同的代码在调试版本和发布版本之间呈现不同的结果 编辑:,c#,linq-to-sql,dbml,C#,Linq To Sql,Dbml,决定是否继续处理潜在客户的if语句未包装在#if DEBUG…#else指令中,因此在所有情况下都应执行该指令 TL;DR当我在调试中运行时,代码的某些部分返回的结果与运行发布版本时的结果不同。为什么会发生这种情况?我该如何解决 我的应用程序处理客户邮件列表上的后续电子邮件。如何做到这一点对本文并不重要,但它是一个WPF应用程序,可以获得所有超过2周的线索,并满足其他一些条件 首先,引线被加载到内存中: public class MainViewModel : BaseViewModel {
if
语句未包装在#if DEBUG…#else
指令中,因此在所有情况下都应执行该指令
TL;DR当我在调试中运行时,代码的某些部分返回的结果与运行发布版本时的结果不同。为什么会发生这种情况?我该如何解决
我的应用程序处理客户邮件列表上的后续电子邮件。如何做到这一点对本文并不重要,但它是一个WPF应用程序,可以获得所有超过2周的线索,并满足其他一些条件
首先,引线被加载到内存中:
public class MainViewModel : BaseViewModel
{
public ContactsViewModel Contacts { get; }
public EmailsViewModel Emails { get; }
public FollowUpsViewModel FollowUps { get; }
public GroupsViewModel Groups { get; }
public LeadsViewModel Leads { get; }
public LeadStatusesViewModel LeadStatuses { get; }
public MainViewModel(LoadingViewModel loadingVM)
{
Groups = new GroupsViewModel();
LeadStatuses = new LeadStatusesViewModel();
Contacts = new ContactsViewModel(Groups);
Emails = new EmailsViewModel(Campaigns, Templates);
Leads = new LeadsViewModel(Campaigns, Emails, LeadStatuses, Groups);
FollowUps = new FollowUpsViewModel(Leads);
}
public async Task Load()
{
// Contacts data is loaded ad hoc when requested.
await Groups.Load();
await Emails.Load();
await LeadStatuses.Load();
await Leads.Load();
// Delegate follow ups to a new thread so as to keep the UI responsive.
new Thread(delegate () { FollowUps.FollowUp(this); }).Start();
}
}
引线加载到引线VIEWMODEL上的可观察集合中:
public class LeadsViewModel : BaseViewModel
{
public ObservableCollection<LeadViewModel> AllLeads = new ObservableCollection<LeadViewModel>();
public LeadsViewModel(CampaignsViewModel campaigns, EmailsViewModel emails, LeadStatusesViewModel statuses, GroupsViewModel groups)
{
// Loads other ObservableCollections... These aren't really relevant to this question for the most part.
_campaigns = campaigns;
_emails = emails;
_statuses = statuses;
_groups = groups;
}
public async Task Load()
{
var contacts = await Dal.Instance.Contacts.GetDictionaryAsync();
var models = await TrackerDal.Instance.Leads.ListAsync();
foreach (var m in models)
{
var lead = new EmailStatus();
lead = m;
// Make VERY sure that the error is that the associated contact doesn't exist.
if (!contacts.TryGetValue(lead.ContactId, out var contact))
{
TrackerDal.Instance.Leads.Delete(lead, false);
}
else
{
// Add the lead.
AllLeads.Add(new LeadViewModel(this, m, _emails, _statuses, _groups, contact));
}
}
}
}
以下是日志文件:
调试日志-这是应用程序在调试器中运行时记录的内容:
2018-07-19 16:25:14701[16]信息跟进开始…2018-07-19 16:25:14745[16]调试[跟进]连接字符串为数据源=ortund;初始目录=ortund;用户id=ortund;密码=gwrhw4h;MultipleActiveResultSets=True
2018-07-19 16:25:14745[16]调试[跟进]检查是否存在有效潜在客户…
2018-07-19 16:25:14747[16]调试[跟进]发现有效线索…
2018-07-19 16:25:14748[16]调试[跟进]找到2条有效线索进行处理…
2018-07-19 16:25:14749[16]调试[跟进]启动lead#1
2018-07-19 16:25:14798[16]调试[跟进]发送lead#1的跟进电子邮件
2018-07-19 16:25:15078[16]调试[跟进]线索1在数据库中更新
2018-07-19 16:25:15078[16]调试[跟进]线索#1处理完成。
2018-07-19 16:25:15078[16]调试[跟进]启动lead#2
2018-07-19 16:25:15080[16]调试[跟进]发送lead#2的跟进电子邮件
2018-07-19 16:25:15155[16]调试[跟进]线索2在数据库中更新
2018-07-19 16:25:15157[16]调试[跟进]潜在客户2处理完成 信息日志-此日志在发布生成和发布后记录: 2018-07-19 16:27:57562[16]信息跟进开始…
2018-07-19 16:27:57629[16]信息[后续]连接字符串为数据源=ortund;初始目录=ortund;用户id=ortund;密码=gwrhw4h;MultipleActiveResultSets=True
2018-07-19 16:27:57629[16]信息[跟进]检查是否存在有效潜在客户…
2018-07-19 16:27:57630[16]信息未找到
这不是很明显吗?在发行版上运行将关闭所有调试消息,因为未定义
#debug
-变量。这是发布版本构建的主要目的。那么为什么if(_leads.AllLeads.Where(x=>x.Email!=null).Count()>=1)
在调试时返回true
,但在发布时返回false
?您是否尝试过使用调试器在项目属性中单步执行发布版本,您可以在发布版本中定义调试常量-如果这解决了问题,那么您知道它与条件编译有关,如果不是,那么它是另一回事-检查项目属性中的版本之间是否有任何差异。@Neil还谈到日志记录,我这样做是为了看看这个版本的执行是否与调试不同。.Instance
变量存在,因为我的和不同的级别被处理。。。理想情况下,我希望调试日志放在调试日志文件中,而信息日志放在信息日志文件中。。。这当前将所有内容都放在所有文件中。。。但这实际上与问题无关。
public class FollowUpsViewModel : BaseViewModel
{
private readonly LeadsViewModel _leads;
public FollowUpsViewModel(LeadsViewModel leads)
{
_leads = leads;
}
public void FollowUp(MainViewModel mvm)
{
try
{
UpdateMessage(mvm, $"Follow up started.");
Logger.Instance.LogInfo("Follow ups started...");
int leadNumber = 1;
int leadsCount = 0;
#if DEBUG
Logger.Instance.LogDebug($"[Follow Up] Connection string is {TrackerDal.Instance.ConnectionString}");
#else
Logger.Instance.LogInfo($"[Follow Up] Connection string is {TrackerDal.Instance.ConnectionString}");
#endif
#if DEBUG
Logger.Instance.LogDebug("[Follow Up] Checking valid leads exist...");
#else
Logger.Instance.LogInfo("[Follow Up] Checking valid leads exist...");
#endif
if (_leads.AllLeads.Where(x => x.Email != null).Count() >= 1)
{
// At this point, the code loops through existing leads to send follow up emails and updates the database.
}
else
{
#if DEBUG
Logger.Instance.LogDebug("None found...");
#else
Logger.Instance.LogInfo("None found...");
#endif
}
}
catch(Exception ex)
{
// Do error handling stuff
}
}