我想在客户端和服务器端的 blazor web assembly net 6 应用程序中使用 serilog。在本文中,我了解了如何将日志条目中继到服务器,以便将它们写入日志文件中。
然而,在此方法中,Log静态类用于显式添加日志条目。
我想添加 serilog 作为日志记录提供程序,以便也记录异常和自动生成的信息。
在服务器端我使用
var builder = WebApplication.CreateBuilder(args);
builder.Host
.UseSerilog((ctx, lc) =>
{
lc.ReadFrom.Configuration(ctx.Configuration);
});
Run Code Online (Sandbox Code Playgroud)
这样所有的东西都会传递给serilog。
UseSerilog在 Serilog.AspNetCore 中定义。不幸的是,如果我将 Serilog.AspNetCore 添加到我的客户端项目中,则会出现以下错误:
NETSDK1082 没有可用于指定 RuntimeIdentifier“browser-wasm”的 Microsoft.AspNetCore.App 运行时包
有没有办法手动将 serilog 添加到日志记录提供程序,或者有没有办法将 Serilog.AspNetCore 包添加到客户端项目?
在Xamarin.Forms中,我想将属性后面的代码绑定到XAML中的标签。
我找到了许多有关此主题的答案和网页,但它们都涵盖了更复杂的场景。
这是我的XAML页面:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:TrackValigie"
x:Class="TrackValigie.SelViaggioPage">
<ContentPage.Content>
<StackLayout>
<Label Text="{Binding ?????????}" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)
这是背后的代码:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SelViaggioPage : ContentPage
{
private string _lblText;
public string LblText
{
get
{
return _lblText;
}
set
{
_lblText = value;
OnPropertyChanged();
}
}
public SelViaggioPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
this.LblText = "Ciao!!";
base.OnAppearing();
}
}
Run Code Online (Sandbox Code Playgroud)
我想仅使用XAML将“ LblText”属性绑定到标签,这意味着无需在后面的代码中设置绑定或绑定上下文。
这可能吗?
在 Xamarin Forms 中,我试图创建一个带有属性的 xaml 转换器。例如,这将用于根据属性背后的代码以不同方式显示列表中的值。
我的代码基于此:https : //stackoverflow.com/a/29869734。
转换器:
namespace App2.Converters
{
class MyConverter : IValueConverter
{
public int ConvParam { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return $"value: {value} - ConvParam: {ConvParam}";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
Run Code Online (Sandbox Code Playgroud)
XAML:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:conv="clr-namespace:App2.Converters"
x:Class="App2.MainPage"
x:Name="MainPageXaml">
<ContentPage.Resources>
<conv:MyConverter x:Key="cnv" ConvParam="{Binding Source={Reference MainPageXaml}, Path=PropParam}" />
<!--<conv:MyConverter x:Key="cnv" ConvParam="333" …Run Code Online (Sandbox Code Playgroud) 我有两个表(OldVals 和 NewVals),两者都具有相同的格式:
PK ProductID int
PK CustomerID int
Value decimal
Run Code Online (Sandbox Code Playgroud)
一些示例数据:
老瓦尔斯:
ProductID CustomerID Value
1 1000 10
2 1000 20
3 1000 30
1 2000 40
4 2000 50
2 3000 60
3 3000 70
4 3000 80
Run Code Online (Sandbox Code Playgroud)
新瓦尔斯:
ProductID CustomerID Value
1 1000 50
2 1000 60
1 2000 70
2 3000 80
3 3000 90
Run Code Online (Sandbox Code Playgroud)
我正在尝试查询这两个表并获取:CustomerID、OldVals 中每个客户的值总和、NewVals 中每个客户的值总和。ProductID 被忽略。
给定样本数据的结果将是:
CustomerID SumOld SumNew
1000 60 110
2000 90 70
3000 210 170
Run Code Online (Sandbox Code Playgroud)
通过这个查询:
select
OldVals.CustomerID,
sum(distinct …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我有一个Dictionary<ContainerControl, int>. 我需要检查字典中是否存在某个键,如果找到键则更改其相应的值,或者如果尚不存在则添加该键。我的字典的键是 ControlContainer 对象。我可以使用这个方法:
var dict = new Dictionary<ContainerControl, int>();
/*...*/
var c = GetControl();
if (dict.ContainsKey(c))
{
dict[c] = dict[c] + 1;
}
else
{
dict.Add(c, 0);
}
Run Code Online (Sandbox Code Playgroud)
但我认为这样如果键已经存在,我的字典就会迭代三次:一次在 ContainsKey 中,两次在 if 分支中。
我想知道是否有更有效的方法来做到这一点,比如
var dict = new Dictionary<ContainerControl, int>();
/*...*/
var c = GetControl();
var kvp = dict.GetKeyValuePair(c); /* there is no such function in Dictionary */
if (kvp != null)
{
kvp.Value++;
}
else
{
dict.Add(c, 0);
}
Run Code Online (Sandbox Code Playgroud)
使用 linq 可以做到这一点:
var kvp = …Run Code Online (Sandbox Code Playgroud) 假设我有一个 char 变量,并且根据它的值,我需要执行一些函数,遵循以下模式:
Value | Execute
-------+------------
A | Func1
B | Func2
C | Func3
A or C | Func4
D | Func5
B or D | Func6
-------+------------
Run Code Online (Sandbox Code Playgroud)
例如,如果 v='A' 我将运行 Func1 和 Func4;如果 v='B' 我将运行 Func2 和 Func6。
我正在寻找这样的东西:
switch (v)
{
case 'A': Func1(); break;
case 'B': Func2(); break;
case 'C': Func3(); break;
case 'A': case 'C': Func4(); break;
case 'D': Func5(); break;
case 'B': case 'D': Func6(); break;
}
Run Code Online (Sandbox Code Playgroud)
c#中有类似的东西吗?