小编Ant*_*lov的帖子

如何像log4net一样滚动Serilog文本文件?

我不知道如何设置 Serilog.Sinks.File 来生成此内容:

log.txt <-- current log
log20200704.txt  <-- rolled over yesterday log
log20200703.txt
Run Code Online (Sandbox Code Playgroud)

代替:

log20200705.txt <-- current log
log20200704.txt  <-- rolled over yesterday log
log20200703.txt
Run Code Online (Sandbox Code Playgroud)

从 log4net 时代起我就习惯了这种行为。

c# logging serilog

9
推荐指数
1
解决办法
646
查看次数

如何根据现有属性引入新属性并修改现有属性?

我试图自动化这个XmlSerializer解决方案模式.请参阅下面的更新

是否可以基于现有属性引入新属性并使用PostSharp(或者其他一些AOP工具)修改现有属性的属性?

最好在构建时进行这种修改.

示例源属性:

public class TestType {
  // Original version
  [XmlAttribute()]
  public DateTime ReqDateTime {
      get { return this.reqDateTimeField; }
      set { this.reqDateTimeField = value; }
  }
}
Run Code Online (Sandbox Code Playgroud)

期望的结果(省略类声明):

// Modified version
// <original property> = "ReqDateTime"
// <original property> marked as XmlIgnore
// New property with name "<original property>ForXml" is introduced with code as per below
// XmlAttribute moved to the newly introduced <original property>ForXml property with parameter "<original property>" 
[XmlIgnore()]
public DateTime ReqDateTime { …
Run Code Online (Sandbox Code Playgroud)

c# aop postsharp fody

6
推荐指数
0
解决办法
238
查看次数

是否可以在目标为4.7.1的WinForms项目中使用Path.GetRelativePath(.NET Core2)?

这似乎是一个非常简单的问题,但是使用谷歌搜索没有任何用处。

我有针对.NET Framework 4.7.1的VS2017 WinForms C#项目。

我想利用Path.GetRelativePath.NET核心2.X

它是可以实现的(裸包或其他东西)吗?

PS。对于那些不愿自己移植.NET Core代码的人,这里是我的改编版

.net c# .net-core

6
推荐指数
1
解决办法
1106
查看次数

如何获取PowerShell静态类方法中的当前类名/对象?

我需要$this在静态类中工作!怎么实现呢?任何解决方法?我已经分析Get-PSCallStack了类上下文的返回,发现没什么用处.

我需要这个用于(a)日志记录和(b)调用同一类的其他静态方法,而不是一次又一次地提及它的名字.

示例代码(PowerShell v5):

class foo {
    static [void]DoSomething() {
        [foo]::DoAnything()  #works

        #$this.DoAnything   #not working

        $static_this = [foo]
        $static_this::DoAnything() #works

    }
    static [void]DoAnything() {
        echo "Done"
    }
}

[foo]::DoSomething()
Run Code Online (Sandbox Code Playgroud)

reflection powershell

5
推荐指数
1
解决办法
1008
查看次数

如何重命名/移动项目并覆盖即使它们存在(对于文件,文件夹和链接)?

是否有一个命令可以移动项目(或者只是重命名为源和目标都在一个文件夹中),强制覆盖对任何项目类型(叶子,容器)都有效?

背景:我正在编写一个脚本,用相应的相关符号链接替换所有硬链接和联结.示例代码:

mkdir C:\Temp\foo -ErrorAction SilentlyContinue
'example' > C:\Temp\foo\bar.txt
cd C:\Temp
New-Item -ItemType Junction -Name bar -Target C:\Temp\foo
New-Item -ItemType SymbolicLink -Name bar2 -Target '.\foo'

# Produces error: Rename-Item : Cannot create a file when that file already exists.
Rename-Item -Path 'C:\Temp\bar2' -newName 'bar' -force

# Unexpected behaviour: moves bar2 inside bar
Move-item -Path 'C:\Temp\bar2' -destination 'C:\Temp\bar' -force

# This works as per https://github.com/PowerShell/PowerShell/issues/621
[IO.Directory]::Delete('C:\Temp\bar')
Rename-Item -Path 'C:\Temp\bar2' -newName 'bar'
Run Code Online (Sandbox Code Playgroud)

powershell file-io

5
推荐指数
1
解决办法
4738
查看次数

如何创建相对符号链接powershell方式?

在PowerShell 5.1中创建相对符号链接并不是那么简单.New-Item没有按预期工作.下面列出了一些方法.我错过了什么吗?

所有示例的示例设置:

mkdir C:\Temp\foo -ErrorAction SilentlyContinue
'sample contents' > C:\Temp\foo\foo.txt
cd C:\Temp
Run Code Online (Sandbox Code Playgroud)

Sample1:不能按预期工作

#new ps5 Item cmdlets (https://msdn.microsoft.com/en-us/powershell/wmf/5.0/feedback_symbolic) are not working well with relative paths
#C:\Temp\foo and C:\Temp\foo\foo.txt are returned
$fld = New-Item -ItemType SymbolicLink -Name 'bar' -Target '.\foo'
$fl = New-Item -ItemType SymbolicLink -Name 'bar.txt' -Target '.\foo\foo.txt'
$fld.Target
$fl.Target
Run Code Online (Sandbox Code Playgroud)

Sample2:不能按预期工作

#Powershell community extensions
#same problem - paths are created as absolute: C:\Temp\foo C:\Temp\foo\foo.txt
$fld = New-Symlink 'c:\Temp\bar' '.\foo'
$fl = …
Run Code Online (Sandbox Code Playgroud)

powershell file-io

5
推荐指数
1
解决办法
638
查看次数

如何在不使用attrib.exe的情况下通过Powershell更改扩展的Windows文件属性?

这似乎是一个非常简单的问题,但谷歌搜索没有给我什么.这是错误(PS 5.1,胜利10.0.14393 x64):

Set-ItemProperty $myFileInfo -Name Attributes -Value ([System.IO.FileAttributes]::Temporary)
The attribute cannot be set because attributes are not supported. Only the following attributes can be set: Archive, Hidden, Normal, ReadOnly, or System.
Run Code Online (Sandbox Code Playgroud)

attrib.exe似乎支持大部分System.IO.FileAttributes.不幸的是,似乎不适用于使用FileSystem PSDrives引用的文件.这就是我广泛使用的内容.

SetFileAttributes内核API调用创建包装器将是最后的选择.

我是否缺少设置这些扩展文件属性的其他[更简单]方法?

PS.除了[System.IO.FileAttributes]::Temporary我对设置感兴趣[System.IO.FileAttributes]::NotContentIndexed.

filesystems powershell winapi

1
推荐指数
1
解决办法
919
查看次数